Last active
December 21, 2015 13:38
-
-
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
This file contains 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 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