Created
April 27, 2011 23:46
-
-
Save shurizzle/945492 to your computer and use it in GitHub Desktop.
cooler redefine method
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 Object | |
def redefine_method(meth) | |
return unless block_given? | |
oldmeth = self.instance_method(meth) | |
self.send(:define_method, meth) {|*args| | |
yield(oldmeth.bind(self), *args) | |
} | |
end | |
end | |
#=begin EXAMPLE | |
str = "cacca" | |
class String | |
def puts | |
STDOUT.puts self | |
end | |
end | |
str.puts | |
class String | |
redefine_method(:puts) {|old| | |
STDOUT.print "Hi, i'm " | |
old.call | |
} | |
end | |
str.puts | |
#=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment