Created
April 15, 2016 18:20
-
-
Save rewinfrey/3743aea52152e9b2855b8bbf05131e59 to your computer and use it in GitHub Desktop.
Ruby prepend simple example
This file contains hidden or 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 Example | |
def something | |
puts "in example" | |
super | |
end | |
end | |
class Test | |
prepend Example | |
def something | |
puts "in test" | |
end | |
end | |
# Prepend does what we would intuitively expect: the prepended module is the first ancestor for the sake of method lookup, | |
# and is before the prepending class in the ancestry tree. | |
Test.ancestors # => [Example, Test, Object, Kernel, BasicObject] | |
Test.new.something | |
# >> in example | |
# >> in test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment