public
Last active

cooler redefine method

  • Download Gist
gistfile1.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.