Skip to content

Instantly share code, notes, and snippets.

@YanhaoYang
Created May 21, 2016 13:04
Show Gist options
  • Save YanhaoYang/c96f32917b4c1ae5428b9bf85dd69f03 to your computer and use it in GitHub Desktop.
Save YanhaoYang/c96f32917b4c1ae5428b9bf85dd69f03 to your computer and use it in GitHub Desktop.
Monkey patch by prepend
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