Created
September 16, 2015 12:23
-
-
Save closer/bd3fce4a600758bb8b82 to your computer and use it in GitHub Desktop.
Ruby で super を呼んだ時の呼ばれる順番 ref: http://qiita.com/closer/items/5b90aaf115b4d113fe2a
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
("A".."H").each do |name| | |
eval <<-MODULE | |
module #{name} | |
def hoge | |
"#{name} -> " + super | |
end | |
end | |
MODULE | |
end | |
class Base | |
def hoge | |
"Base" | |
end | |
end | |
class TestClass < Base | |
include A | |
include B | |
def hoge | |
"TestClass -> " + super | |
end | |
include C | |
include D | |
end | |
t = TestClass.new | |
t.extend E | |
t.extend F | |
def t.hoge | |
"t -> " + super | |
end | |
t.extend G | |
t.extend H | |
p t.hoge #=> "t -> H -> G -> F -> E -> TestClass -> D -> C -> B -> A -> Base" | |
class TestClass2 < TestClass | |
include A | |
include B | |
def hoge | |
"TestClass2 -> " + super | |
end | |
include C | |
include D | |
end | |
t = TestClass2.new | |
p t.hoge #=> "TestClass2 -> TestClass -> D -> C -> B -> A -> Base" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment