Skip to content

Instantly share code, notes, and snippets.

@sergio1990
Created December 6, 2014 18:45
Show Gist options
  • Save sergio1990/8737d69a614ac54056cf to your computer and use it in GitHub Desktop.
Save sergio1990/8737d69a614ac54056cf to your computer and use it in GitHub Desktop.
Difference between including and prepending modules into class
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