Created
December 6, 2014 18:45
-
-
Save sergio1990/8737d69a614ac54056cf to your computer and use it in GitHub Desktop.
Difference between including and prepending modules into class
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
puts '===> INCLUDE EXAMPLE' | |
module M1 | |
def run | |
puts 'M1 run' | |
end | |
end | |
class C1 | |
include M1 | |
def run | |
puts 'C1 run' | |
end | |
end | |
puts C1.ancestors.join ', ' | |
C1.new.run | |
puts '===> PREPEND EXAMPLE' | |
module M2 | |
def run | |
puts 'M2 run' | |
end | |
end | |
class C2 | |
prepend M2 | |
def run | |
puts 'C2 run' | |
end | |
end | |
puts C2.ancestors.join ', ' | |
C2.new.run | |
# Output: | |
# | |
# ===> INCLUDE EXAMPLE | |
# C1, M1, Object, Kernel, BasicObject | |
# C1 run | |
# ===> PREPEND EXAMPLE | |
# M2, C2, Object, Kernel, BasicObject | |
# M2 run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment