Created
April 24, 2016 13:19
-
-
Save johnlane/03e5f0dd2d478d2978f35886941c0218 to your computer and use it in GitHub Desktop.
StackOverflow#36799396
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
# 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