Skip to content

Instantly share code, notes, and snippets.

@latentflip
Created December 18, 2012 16:07
Show Gist options
  • Save latentflip/4329281 to your computer and use it in GitHub Desktop.
Save latentflip/4329281 to your computer and use it in GitHub Desktop.
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