The Best of method_missing #
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.
>> require 'xmlrpc/client' >> system = XMLRPC::Client. new2( "http://www.oreillynet.com/meerkat/xml-rpc/server.php" ). proxy( "system" ) >> system.listMethods => ["meerkat.getChannels", "meerkat.getCategories", ...]
Naturally, DRb does similarly. - Builder::XmlBase translates method calls into XML tags.
>> require 'rubygems' >> require_gem 'builder' >> builder = Builder::XmlMarkup.new(:target=>STDOUT, :indent=>2) >> builder.person { |b| b.name("Jim"); b.phone("555-1234") } <person> <name>Jim</name> <phone>555-1234</phone> </person>
- 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.
scott
I use it in my Rails app, Elite Journal to generate RSS and Atom feeds for users:
Such that /rss/scott gets the feed for user scott, and so on and so forth. Not mind blowing really, but nice.
scott
hmm, does pre/code not work in comments? Sorry :(
Richard Dale
The QtRuby/Korundum bindings for the Qt and KDE apis use method_missing to interface with the ‘Smoke’ library. There are over 1000 classes and 30000 methods accessible from ruby via method_missing and its friend const_missing.
method_missing is also used to divert method invocations to KDE DCOP calls – the KDE equivalent to Distributed Ruby DRb. When you invoke a method on a KDE ::DCOPRef the Korundum runtime first looks for it in the Smoke runtime, and if it can’t be found, it catches an exception and tries to call a remote method via DCOP . So here in 70 lines of ruby code we interface with both DCOP and Smoke with a nice combination of method_missing and exception handling. If you use method_missing you can transform method names, and so CamelCase or all lower case with underscores can be used. And a DCOP method call ‘setThing()’ can be invoked via ‘thing=’ in ruby:
why
Whoa, man. I gotta get these code tags under CONTROL !! I may ditch the borders. It’s too bad the scrollbars on CSS overflow is so poorly contrived.
tim
In RMagick you can call any Image method on an ImageList object. In ImageList, method_missing routes the method on to the current image in the list. Following the RubyStyleGuide I also overrode respond_to? to give consistent results.
francis
I’d imagine ActiveRecord does the same.
I was pretty impressed by Rich Kilmer’s Alph presentation at RubyConf 2004, which transparently dispatches ActionScript method calls from Ruby to a Flash movie over the network. Basically abstracting away the entire network, like it was some piddling implementation detail … sw33t.
Asenchi
the code tags are blinking at me, ... kind of freaking me out.
flgr
I’m drunk, but RedHanded still rocks!
Matt
Unit testing—I pass a class that records the methods called upon it (via method_missing) and assert that the right things are called in the right order.
norseman
Is that what that is Asenchi? I thought _why was trying to send subliminal ruby messages to the brain…
flgr
I think the code blinking might be related to the refreshing time that is used at the comment posting form. Perhaps the timeout could be set closer to a minute?
why
Hey, you don’t sound drunk. You sound sensible and very rational. In fact, I took your suggestion right away. Soon as I saw it.
william
RubyTorrent uses
method_missing
to make all the bencoded fields in the .torrent file easy to access. (bencoding being the BitTorrent encoding scheme.)This is basically identical to the Javascript attributes thing above, but with some type-checking. For example:
richard
I’m sorry my method_missing code went missing.
joar054
This is a rather cute idea I took from PickAxe 1. For example, Roman [10] returns “X” and Roman.CLIX returns 159(through method_missing).
Asenchi
_why, maybe remove the sidebar when comments are shown in order to get rid of some of these scroll bars and have a larger space?
Just an idea.
daigo
Needle’s container.rb.
Comments are closed for this entry.