Skip to content

Instantly share code, notes, and snippets.

@seanhandley
Created October 10, 2010 20:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanhandley/619554 to your computer and use it in GitHub Desktop.
Save seanhandley/619554 to your computer and use it in GitHub Desktop.
class Object
def method_missing(symbol, *args, &block)
if(args.nil?)
symbol.to_s
else
"#{symbol.to_s} #{args[0].to_s}"
end
end
end
puts "the quick brown fox jumps over the lazy dog"
puts the quick brown fox jumps over the lazy dog
# Outputs:
# >> the quick brown fox jumps over the lazy dog
# >> the quick brown fox jumps over the lazy dog
@seanhandley
Copy link
Author

One of the reasons I absolutely love Ruby. Metaprogramming let's you re-open the Object base-class, override the method_missing method and have it build a string on the fly from undefined, anonymous method signatures, recursively, taking each anonymously defined token and using its string conversion as the parameter for the next. Magic.

@raldred
Copy link

raldred commented Oct 10, 2010

This is basic stuff really. Watch Meta programming by Dave Thomas for some even cooler stuff.

@seanhandley
Copy link
Author

I've had his screencast series on metaprogramming for ages now. Very cool stuff indeed. This is a simple example from the Ruby class we've been doing at OTB, posted mainly for the benefit of the non Ruby programmers following :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment