Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Last active May 21, 2016 09:21
Show Gist options
  • Save YanhaoYang/244f896ada5d032e15531ee185806490 to your computer and use it in GitHub Desktop.
Save YanhaoYang/244f896ada5d032e15531ee185806490 to your computer and use it in GitHub Desktop.
Monkey patch by alias_method
class A
def hi
puts "hi"
end
end
a = A.new
a.hi
# => hi
module M1
def self.included(base)
base.send :alias_method, :hi_original, :hi
base.send :alias_method, :hi, :hi_in_m1
end
def hi_in_m1
puts "hi in M1"
end
end
A.send :include, M1
a.hi
# => hi in M1
a.hi_original
# => hi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment