Created
October 10, 2010 20:51
-
-
Save seanhandley/619554 to your computer and use it in GitHub Desktop.
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
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 |
This is basic stuff really. Watch Meta programming by Dave Thomas for some even cooler stuff.
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
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.