Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Ruby is able to have much simpler lookup rules because it never considers the instance, only ever the class hierarchy.

“But!” I can hear people say, “I heard you could attach methods to instances in Ruby! How is this possible if Ruby never considers the instance in resolving a method call?”

Well, that’s tricky. The thing is, Ruby instances have no public members, instance variables are private (for direct access, but not in any strict way, because there are public methods called instance_variable_get and instance_variable_get on Object which…do exactly what the names say.)

But every Ruby instance conceptually has (though it doesn’t concretely have one unless you add something to it) a unique class for the instance that is the first thing in its class heirarchy. And you can add methods to that class (the instance “metaclass”, which is different than a Python metaclass) for the effect of attaching them uniquely to the instance itself.



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: