Last active
December 26, 2015 22:59
-
-
Save tatat/7227585 to your computer and use it in GitHub Desktop.
メソッド入れ替えマン
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
class Nyan | |
def replace_method(a, b) | |
(class << self; self end).module_exec do | |
alias_method "#{a}_backup", a | |
alias_method a, b | |
alias_method b, "#{a}_backup" | |
end | |
end | |
def method1 | |
:method1 | |
end | |
def method2 | |
:method2 | |
end | |
end | |
nyan = Nyan.new | |
p [nyan.method1, nyan.method2] #=> [:method1, :method2] | |
nyan.replace_method :method1, :method2 | |
p [nyan.method1, nyan.method2] #=> [:method2, :method1] | |
nyan.replace_method :method1, :method2 | |
p [nyan.method1, nyan.method2] #=> [:method1, :method2] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment