Skip to content

Instantly share code, notes, and snippets.

@knugie knugie/meta_module3.rb forked from wteuber/meta_module2.rb
Last active Dec 21, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.