Last active
May 21, 2016 09:21
-
-
Save YanhaoYang/244f896ada5d032e15531ee185806490 to your computer and use it in GitHub Desktop.
Monkey patch by alias_method
This file contains hidden or 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 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