Created
November 25, 2014 16:24
-
-
Save stevecass/d718290951618a52f9f4 to your computer and use it in GitHub Desktop.
Ancestor chaining in ruby
This file contains hidden or 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 AAA | |
def abc | |
"abc in AAA" | |
end | |
def not_in_common | |
"not_in_common in AAA" | |
end | |
end | |
module BBB | |
def abc | |
"abc in BBB" | |
end | |
end | |
class TestClass | |
include AAA, BBB | |
end | |
class SeparateIncludes | |
include AAA | |
include BBB | |
end | |
t = TestClass.new | |
puts | |
puts "Ancestors of #{t.class} are #{t.class.ancestors}" | |
puts "TestClass#abc returns \"#{t.abc}\"" | |
puts | |
puts "Includes on separate lines behave differently" | |
puts "SeparateIncludes ancestors: #{SeparateIncludes.ancestors}" | |
puts | |
puts "SeparateIncludes#abc returns \"#{SeparateIncludes.new.abc}\"" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output
Method lookup proceeds up the ancestor chain.