Skip to content

Instantly share code, notes, and snippets.

@halferty
Created August 28, 2013 17:18
Show Gist options
  • Save halferty/6368646 to your computer and use it in GitHub Desktop.
Save halferty/6368646 to your computer and use it in GitHub Desktop.
Ruby metaprogramming awesomeness
class Foo
def method_missing(method_name, *args)
puts "There's no method here by the name #{method_name}. You suck!\n\n"
singleton_class.superclass.send :define_method, method_name do |*args|
puts "I lied. This method was totally here all along."
puts "Look, I can see the arguments: #{args.join ' '}\n\n"
end
end
end
my_foo = Foo.new
my_foo.do_some_crazy_stuff_with_horses("palomino", "pinto", "pony")
my_foo.do_some_crazy_stuff_with_horses("palomino", "pinto", "pony")
my_other_foo = Foo.new
my_other_foo.do_some_crazy_stuff_with_horses("palomino", "pinto", "pony")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment