Skip to content

Instantly share code, notes, and snippets.

@shurizzle
Created April 27, 2011 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shurizzle/945492 to your computer and use it in GitHub Desktop.
Save shurizzle/945492 to your computer and use it in GitHub Desktop.
cooler redefine method
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