Created
December 18, 2012 16:07
-
-
Save latentflip/4329281 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 MagicObject | |
def method_missing(name, *args, &block) | |
if name =~ /greet_/ | |
puts "Haven't greeted this person before, I'll make them a greet method" | |
self.class.send(:define_method, name) do | |
puts "Yo! #{name.to_s.split('_',2)[1].gsub('_',' ')}" | |
end | |
self.send(name) | |
else | |
super | |
end | |
end | |
end | |
my_object = MagicObject.new | |
my_object.greet_phil | |
#=> "Haven't greeted this person before, I'll make them a greet method" | |
#=> "Yo! phil" | |
my_object.greet_phil | |
#=> "Yo! phil" | |
my_object.greet_markos_charatzas | |
#=> "Haven't greeted this person before, I'll make them a greet method" | |
#=> "Yo! markos charatzas" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment