Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created April 15, 2016 18:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rewinfrey/3743aea52152e9b2855b8bbf05131e59 to your computer and use it in GitHub Desktop.
Save rewinfrey/3743aea52152e9b2855b8bbf05131e59 to your computer and use it in GitHub Desktop.
Ruby prepend simple example
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