Skip to content

Instantly share code, notes, and snippets.

@tatat
Last active December 26, 2015 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tatat/7227585 to your computer and use it in GitHub Desktop.
Save tatat/7227585 to your computer and use it in GitHub Desktop.
メソッド入れ替えマン
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