This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: The Best of method_missing
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
I never use method_missing. Maybe twice. And both times I didn’t use it, regretted it, forcefully ejected the code from a moving vehicle, shed nary a tear.
And yet, there’s so many great uses for method_missing out there. If I may, a few of my favorite. And if you will, please, a few of yours?
XMLRPC::Client::Proxy will pass its method calls directly onto the service. You can even specific a prefix, which makes the whole thing a little more psuedo-OO.
Hash with Attrs (halfway down the page) is kind of neat. Make your Hashes act like JavaScript objects.
class Hash
def method_missing(meth,*args)
if /=$/=~(meth=meth.id2name) then
self[meth[0...-1]] = (args.length<2 ? args[0] : args)
else
self[meth]
end
end
end
How do you use method_missing? Blow our minds post-haste.