Created
May 21, 2016 13:04
-
-
Save YanhaoYang/c96f32917b4c1ae5428b9bf85dd69f03 to your computer and use it in GitHub Desktop.
Monkey patch by prepend
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 hi | |
puts 'hi in M1' | |
super | |
end | |
end | |
A.send :prepend, M1 | |
a.hi | |
# => "hi in M1" | |
# => "hi" | |
class A | |
def hi | |
puts 'hi redefined' | |
end | |
end | |
a.h | |
# => hi in M1 | |
# => hi redefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment