Ruby Method Lookup Flow

Ruby method
lookup flow
module Kernel
module Kernel
def self.class_meth def inst_method
# ...
# ...
core ruby objects
class Object
class Object
def self.class_meth def inst_method
# ...
# ...
copyright ©2006 Gavin Kistner
[email protected]
1
class Module
class Module
def self.class_meth def inst_method
# ...
# ...
class Class
class Class
def self.class_meth def inst_method
# ...
# ...
module Polygon
module Polygon
def self.class_meth def inst_method
# ...
# ...
2
3
class Rect
include Polygon
end
class Rect
class Rect
def self.class_meth def inst_method
# ...
# ...
module Regular
module Reguiar
def self.class_meth def inst_method
# ...
# ...
4
class Square < Rect class Square < Rect
def self.class_meth def inst_method
# ...
# ...
class Square
include Regular
end
def square1.meth
#just square1
end
square1.do_it( )
key
classes
modules
instances
square2.do_it( )
1
All method lookups end up at the instance
methods of Object (which includes Kernel).
This includes class methods of user classes.
3
Including a module in a class only pulls in the
instance methods, not the class methods.
(Using extend instead would cause the class to
inherit class methods from the instance methods
of the module.)
2
User classes inherit class methods from the
instance methods on the Class class, after
going through class methods of Object.
4
Including a module in a sub class can 'shadow'
methods from the parent class, as the module is
searched before the parent.