Ruby's Object Model
Inheritance Hierarchy
By default, Ruby classes inherit directly from Object
:
Foo.ancestors.inspect
shows us that Foo
inherits from Object
.
Effects of
include
Include a modules methods as instance methods in the current Module
/Class
extend
Include a modules methods as class methods in the current Module
/Class
prepend
- Prepend the module to the class’s ancestry
- Prepended modules methods can’t be (acidentally) overwritten (unlike included modules)
super
Calls the parent classes version of the current method
Method Dispatch
Ruby looks up methods in the following order:
- Methods defined in the object’s singleton class (i.e. the object itself)
- Modules mixed into the singleton class in reverse order of inclusion
- Methods defined by the object’s class
- Modules included into the object’s class in reverse order of inclusion
- Methods defined by the object’s superclass, i.e. inherited methods
Sources / Things to look at
Practicing Ruby:
Misc Blogs: