The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
The Best of method_missing

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
The Best of method_missing Posted: Feb 3, 2005 4:08 PM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

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") }
     
       Jim
       555-1234
     
    
  • 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.

Read: The Best of method_missing

Topic: If You Buy One Crossstitch This Year Previous Topic   Next Topic Topic: Writing C -- in Ruby!


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2017 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us