Oh, the places you’ll go…


Inspired from the Practicing Ruby entry, I somewhat clarified the code a little (for my taste) and learned that the call stack in Ruby is:

0) Undefined method resolution
1) Methods defined in the object’s singleton class (i.e. the object itself)
2) Modules mixed into the singleton class in reverse order of inclusion
3) Methods defined by the object’s class
4) Modules included into the object’s class in reverse order of inclusion
5) Methods defined by the object’s superclass, i.e. inherited methods

module ModuleA
 def foo
   "- Mixed in method defined by ModuleA\n" + super
 end
end  
module ModuleB
  def foo
   "- Mixed in method defined by ModuleB\n" + super
  end
end  
module ModuleC
  def foo
   "- Extended in method defined by ModuleC\n" + super
 end
end  
module ModuleD
  def foo
   "- Extended in method defined by ModuleD\n" + super
 end
end  
class A
 def foo
   "- Instance method defined by A\n"
 end
end  
class B < A
 include ModuleA
 include ModuleB
 def foo
    "- Instance method defined by B\n" + super
 end  
  def method_missing(method)
   puts "- method_missing (#{method}) on b. Redeirecting to b.foo\n"
   foo
 end
end  
b = B.new
b.extend(ModuleC)
b.extend(ModuleD)
def b.foo
 "- Method defined directly on an instance of B\n" + super
end
def b.method_missing(method)
 "- method_missing (#{method}) on b. Calling super\n" + super
end
puts "Calling 'bar' on b of type #{b.class}:\n"
puts b.bar

Which gives:

~/projects/ita/ruby$ ruby test.rb

Calling ‘bar’ on b of type B:

– method_missing (bar) on b. Redeirecting to b.foo
– method_missing (bar) on b. Calling super
– Method defined directly on an instance of B
– Extended in method defined by ModuleD
– Extended in method defined by ModuleC
– Instance method defined by B
– Mixed in method defined by ModuleB
– Mixed in method defined by ModuleA
– Instance method defined by A

Follow the conversation on Stack Overflow.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s