The document provides an overview of Ruby's object model and metaclasses. It discusses what objects and classes are at a low level, how method dispatch works, and what metaclasses (or singleton classes) are. Key points include:
- Objects have an ivar hash and classes have an ivar hash and methods table
- Method dispatch follows the class pointer, checks the methods table, and follows the super pointer if needed
- Classes are objects that have a methods table and superclass pointer
- All classes have singleton classes/metaclasses that are used to define class methods
- Method definitions on objects add methods to the singleton class
21. method dispatch
1. Follow my class pointer
2. Check the methods table for my method
3. Call the method, or follow the super
pointer if the method doesn’t exist
22. method dispatch
1. Follow my class pointer
2. Check the methods table for my method
3. Call the method, or follow the super
pointer if the method doesn’t exist
4. Repeat 2-3 if the method was not found
23. method dispatch
super
Bar
class
ivar hash
foo class
ivar hash methods
24. method dispatch
super
1. Follow the class pointer Bar
class
ivar hash
foo class
ivar hash methods
25. method dispatch
super
1. Follow the class pointer Bar
class
ivar hash
foo class
ivar hash methods
2. Check the method table
26. method dispatch
super
3. Follow the super pointer
1. Follow the class pointer Bar
class
ivar hash
foo class
ivar hash methods
2. Check the method table
27. class Primate
def opposable_thumbs
true
end
end
bob = Primate.new
bob.opposable_thumbs # => true
45. Object
super
Primate
joe.opposable_thumbs
super
class
joe joe'
46. Object
super
Primate
joe.opposable_thumbs
super
class
joe joe'
47. Object
super
Primate
joe.opposable_thumbs
super
class meth
joe joe' ods
48. Object
super
Primate
joe.opposable_thumbs
super
class meth
joe joe' ods
#opposable_thumbs
49. Object
super
Primate
joe.opposable_thumbs
super
class meth
joe joe' ods
#opposable_thumbs
# => false :)
50. Object
super
meth
Primate ods
joe.opposable_thumbs
super
class meth
joe joe' ods
#opposable_thumbs
# => false :)
51. Object
super
meth
Primate ods
#opposable_thumbs
joe.opposable_thumbs
super
class meth
joe joe' ods
#opposable_thumbs
# => false :)
52. Object
super
meth
Primate ods
#opposable_thumbs
joe.opposable_thumbs # => true
super
class meth
joe joe' ods
#opposable_thumbs
# => false :)
53. Object
super
class meth
bob Primate ods
#opposable_thumbs
joe.opposable_thumbs # => true
super
class meth
joe joe' ods
#opposable_thumbs
# => false :)
54. Object
super
Primate
super
Joe’s Singleton Class
class
joe joe'
55. Metaclass
Singleton Class
Eigenclass
Virtual Class
56. Metaclass
Singleton Class
Eigenclass
Virtual Class
79. super
Class
super
class
Object Object'
super
super
class
Primate Primate'
80. super
Class
super
def Object.foo
class
Object Object'
super
super
class
Primate Primate'
81. super
Class
super
def Object.foo
class
Object Object'
super
super
Primate.foo
class
Primate Primate'
82. super
Class
super
def Object.foo
class
Object Object'
super
super
Primate.foo
meth
class ods
Primate Primate'
#species
83. super
Class
super
def Object.foo
meth
ods
class
Object Object' #foo
super
super
Primate.foo
meth
class ods
Primate Primate'
#species
84. super
Module
super
Class
super
class
Object Object'
super
class super
Primate Primate'
85. super
Module
super
Class
super
class
Object Object'
super
super
class
Primate Primate'
super
class class
joe joe'
86. super
Module
super
Kernel Class
super
super
class
Object Object'
super
super
class
Primate Primate'
super
class class
joe joe'
87. super
BasicObject Module
1 .9
super
super
Kernel Class
super
super
class
Object Object'
super
super
class
Primate Primate'
super
class class
joe joe'
89. class Object
# The hidden singleton lurks behind everyone
def metaclass
class << self
self
end
end
def meta_eval(&blk)
metaclass.instance_eval(&blk)
end
# Adds methods to a metaclass
def meta_def(name, &blk)
meta_eval { define_method(name, &blk) }
end
# Defines an instance method within a class
def class_def(name, &blk)
class_eval { define_method(name, &blk) }
end
end http://code.whytheluckystiff.net/metaid/
90. class Object
# The hidden singleton lurks behind everyone
def metaclass
class << self
self
end
end
def meta_eval(&blk)
metaclass.instance_eval(&blk)
end
# Adds methods to a metaclass
def meta_def(name, &blk)
meta_eval { define_method(name, &blk) }
end
# Defines an instance method within a class
def class_def(name, &blk)
class_eval { define_method(name, &blk) }
end
end http://code.whytheluckystiff.net/metaid/
91. class Object
# The hidden singleton lurks behind everyone
def metaclass
class << self
self
end
end
def meta_eval(&blk)
metaclass.instance_eval(&blk)
end
# Adds methods to a metaclass
def meta_def(name, &blk)
meta_eval { define_method(name, &blk) }
end
# Defines an instance method within a class
def class_def(name, &blk)
class_eval { define_method(name, &blk) }
end
end http://code.whytheluckystiff.net/metaid/
93. def method
defines an instance method on Primate
class Primate
# def defines a method on self
def opposable_thumbs
true
end
end
94. def object.method
defines a method on the singleton of Primate
class Primate
# defines a method on the singleton of self
# self is the class Primate
def self.species
["Lemur", "Monkey", "Human"]
end
end
95. class << o; def
defines a method on the singleton of Primate
class << Primate
# in the singleton class of Primate
def species
["Lemur", "Monkey", "Human"]
end
end
96. class <<
class << my_object
# in the context of the
# singleton class of my_object
end
97. class <<
class << my_object
# in the context of the
# singleton class of my_object
end
class Primate
# in the context
# of class Primate
end
98. class Foo
class << self
def bar
"hello"
end
end
end
Foo.bar # => "hello"
99. include vs. extend
module ThumbsUp
def opposable_thumbs; true; end
end
module Species
def species; ["Lemur", "Monkey", "Human"]; end
end
class Primate
# adds methods to the class Primate
include ThumbsUp
# adds methods to the singleton of Primate
extend Species
end
many different names
slight differences in meaning sometimes
singleton class stores instance-specific behavior
created automatically on request (class << obj)
classes are objects
metaclasses store instance specific behavior for classes
ruby magic to hide the singleton
groovy has explicit access to singleton
remember when we defined instance specific methods on joe
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
instance-specific behavior is class methods
dereference class pointer, look up method, climb inheritance chain
class of Primate is class b/c we did Class.new
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
Missing Primate&#x2019;s superclass (Object)
we have part of ruby&#x2019;s object model, lets continue by fixing a problem
ruby creates singletons on-demand, but metaclasses automatically
that includes object
insert object into graph
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
added object&#x2019;s metaclass
object&#x2019;s class methods will not be visible on primate
Primate.inspect
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
Class has a metaclass too, but it&#x2019;s pretty much limited to new
Primate.foo inherits from Object
the metaclass of my superclass is the superclass of my metaclass
what about module?
ruby trickery with joe&#x2019; superclass
look at the diagram, what is Joe&#x2019;s singleton&#x2019;s superclass?
including a module inserts it into the inheritance chain
mention method_missing
ruby hides it, but we can get it out
ruby 1.8 only and does not effect method dispatch
iclass code redacted
ruby 1.8 only
you can take singleton_class
whenever you def object.method, you define a method on the singleton of that object
class << just switches into the context of the singleton class of whatever object you specify
<< = &#x201C;give me ur singleton class&#x201D;
switches into the context of the singleton class of self