Created
August 5, 2011 19:09
-
-
Save trshafer/1128263 to your computer and use it in GitHub Desktop.
Dynamically overwrite methods
This file contains 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 Frog | |
def self.go | |
puts 45 | |
end | |
end | |
Frog.go | |
module Whatever | |
def self.extended(base) | |
(class << base; self; end).send(:alias_method, :go, :new_go) | |
end | |
def new_go | |
puts 99 | |
end | |
end | |
Frog.send(:extend, Whatever) | |
Frog.go |
This file contains 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 Frog | |
def go | |
puts 45 | |
end | |
end | |
Frog.new.go | |
module Whatever | |
def self.included(base) | |
base.send(:alias_method, :go, :new_go) | |
end | |
def new_go | |
puts 99 | |
end | |
end | |
Frog.send(:include, Whatever) | |
Frog.new.go |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment