Skip to content

Instantly share code, notes, and snippets.

@knugie
Last active December 21, 2015 13:38
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 knugie/6313678 to your computer and use it in GitHub Desktop.
Save knugie/6313678 to your computer and use it in GitHub Desktop.
metaprogramming with modules in ruby This will only work with ruby 2.0 http://ruby-doc.org/core-2.0/Module.html#method-i-prepend
module One
def a(*)
puts 'One...'
super
end
end
module Two
def a(*)
puts 'Two...'
super
end
end
class Test
def a(*args)
puts 'Zero'
end
end
Test.new.a
#Zero
Test.class_eval do
prepend One
end
Test.new.a
#One...
#Zero
Test.class_eval do
prepend Two
end
Test.new.a
#Two...
#One...
#Zero
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment