include and prepend in ruby along with super
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
module A | |
def say | |
puts "in A" | |
super | |
end | |
end | |
module B | |
def say | |
puts "in B" | |
super | |
end | |
end | |
class Parent | |
def say | |
puts "in Parent" | |
end | |
end | |
class IncludeParent | |
def say | |
puts "in IncludeParent" | |
end | |
end | |
class PreprendParent | |
def say | |
puts "in PreprendParent" | |
end | |
end | |
class IncludeParent | |
include A, B | |
end | |
class PreprendParent | |
prepend A, B | |
end | |
class Temp < Parent | |
include A, B | |
end | |
# => IncludeParent, A, B, Object, Kernel, BasicObject | |
puts IncludeParent.ancestors | |
puts | |
# => A, B, PreprendParent, Object, Kernel, BasicObject | |
puts PreprendParent.ancestors | |
puts | |
# => Temp, A, B, Parent, Object, Kernel, BasicObject | |
puts Temp.ancestors | |
puts | |
IncludeParent.new.say #=> in IncludeParent | |
puts | |
#=> in A | |
# in B | |
# in PreprendParent | |
PreprendParent.new.say | |
puts | |
#=> in A | |
# in B | |
# in Parent | |
Temp.new.say |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment