Created
August 22, 2014 22:02
-
-
Save edymerchk/3886e57a20482577b243 to your computer and use it in GitHub Desktop.
demonstration of order that ruby methods are look up
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module W | |
def foo | |
"- Mixed in method defined by W\n" + super | |
end | |
end | |
module X | |
def foo | |
"- Mixed in method defined by X\n" + super | |
end | |
end | |
module Y | |
def foo | |
"- Mixed in method defined by Y\n" + super | |
end | |
end | |
module Z | |
def foo | |
"- Mixed in method defined by Z\n" + super | |
end | |
end | |
class A | |
def foo | |
"- Instance method defined by A\n" | |
end | |
end | |
class B < A | |
include W | |
include X | |
def foo | |
"- Instance method defined by B\n" + super | |
end | |
end | |
object = B.new | |
object.extend(Y) | |
object.extend(Z) | |
def object.foo | |
"- Method defined directly on an instance of B\n" + super | |
end | |
puts object.foo | |
- Method defined directly on an instance of B | |
- Mixed in method defined by Z | |
- Mixed in method defined by Y | |
- Instance method defined by B | |
- Mixed in method defined by X | |
- Mixed in method defined by W | |
- Instance method defined by A | |
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. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment