Skip to content

Instantly share code, notes, and snippets.

@johnlane johnlane/test.rb
Created Apr 24, 2016

Embed
What would you like to do?
StackOverflow#36799396
# in-ruby-is-it-possible-to-move-a-module-up-the-ancestor-chain
# http://stackoverflow.com/questions/36799396
module A
def hiya(str)
puts "ho #{str}"
end
def if_what?
end
end
module D
def hiya(str)
puts "hi #{str}"
end
def what_if?
end
end
module B
include D
end
class Base
include A
end
class C1 < Base
include B
end
class C2
include D
end
p C1.ancestors #=> [C1, B, D, A, Object, Kernel, BasicObject]
p C2.ancestors #=> [C2, D, Object, Kernel, BasicObject]
C1.new.hiya("Lois") #=> hi Lois
C2.new.hiya("Lois") #=> hi Lois
p D.instance_methods
(A.instance_methods & D.instance_methods).each { |m| D.send(:remove_method, m) }
p D.instance_methods
C1.new.hiya("Lois") #=> ho Lois
C2.new.hiya("Lois") #=> error - undefined method
#=> not an acceptable solution to the problem.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.