Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Forked from tatat/replace_method.rb
Last active July 8, 2019 20:31
Show Gist options
  • Save Shinpeim/7227597 to your computer and use it in GitHub Desktop.
Save Shinpeim/7227597 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 "#{b}_backup", b
alias_method b, "#{a}_backup"
alias_method a, "#{b}_backup"
end
end
def with_replacing_method(a, b)
replace_method(a, b)
yield
replace_method(b, a)
end
def nyan
:nyan
end
def nyan_extended
:nyan_extended
end
end
nyan = Nyan.new
p nyan.nyan #=> :nyan
nyan.with_replacing_method(:nyan, :nyan_extended) do
p nyan.nyan #=> :nyan_extented
end
p nyan.nyan #=> :nyan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment