Created
September 23, 2011 23:10
-
-
Save trshafer/1238689 to your computer and use it in GitHub Desktop.
Module renaming methods dynamically
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
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