Skip to content

Instantly share code, notes, and snippets.

@trshafer
Created September 23, 2011 23:10
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 trshafer/1238689 to your computer and use it in GitHub Desktop.
Save trshafer/1238689 to your computer and use it in GitHub Desktop.
Module renaming methods dynamically
module Hey
def hi
puts "hi"
end
def sup &block
block.call
end
end
class Dude
include Hey
end
OLD_METHODS = Hey.instance_methods
module Hey
empty_class = Class.new {
include Hey
}.new
OLD_METHODS.each do |method|
old_method = empty_class.method(method)
define_method( "new_#{method}") do |*args, &block|
old_method.call(*args, &block)
end
remove_method method
end
end
OLD_METHODS.each do |method|
begin
Dude.new.send(method)
rescue
puts "no method #{method}"
end
end
Dude.new.new_hi
Dude.new.new_sup do
puts "here"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment