Created
November 17, 2014 14:20
-
-
Save GBH/a0c6e7ec555881242f61 to your computer and use it in GitHub Desktop.
Ruby ancestor chain fun
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 Foo | |
def hello | |
puts 'foo' | |
end | |
end | |
module Bar | |
def hello | |
puts 'bar' | |
end | |
end | |
module Baz | |
def hello | |
puts 'baz' | |
end | |
end | |
class Parent | |
include Foo | |
end | |
class Child < Parent | |
end | |
c = Child.new | |
c.hello # getting 'foo' | |
Parent.send(:include, Bar) | |
c = Child.new | |
c.hello # getting 'bar' | |
Parent.send(:include, Baz) | |
c = Child.new | |
c.hello # getting 'baz' | |
Parent.send(:include, Foo) | |
c = Child.new | |
c.hello # I want 'foo' back | |
puts Child.ancestors.inspect | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment