Chain, Chain, Chain

Posted by Rick DeNatale Sat, 02 Jun 2007 22:28:00 GMT

I’ve noticed some confusion lately on the part of some newcomers to Ruby about how methods are found at runtime, and in particular, the relationship between instance and class methods.

A week or so ago, someone posted some questions on ruby-talk about this issue, and then today I ran across this blog review of David Black’s “Ruby for Rails” which contained this:

On every method call, Ruby will search its object space in the following order:
  1. Current instance, followed by class methods
  2. Mixed in methods
  3. Superclass instance (repeat 1)
  4. Object methods, followed by Kernel mixin

I often get confused with mixin, class, and instance method precedence, so this is a useful model to revisit and keep in mind.

I don’t know where that reference to class methods in item 1 came from. I don’t think David said that in his book.

Here’s how it really works

Here in the form of answers to the questions posed on ruby-talk last week is my explanation of how Ruby finds methods when a message is sent to an object

Read more...

Posted in ,  | Tags , ,  | 1 comment