[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Are my metaprogramming underpants showing?

Matthew Smillie

12/8/2005 12:54:00 AM


> Sorry I didn't get to this until this evening. Hope it's helpful. -T.

Noone's in a hurry over here, so no worries.


> module Flickr
> extend self
>
> @@api = {}
>
> def method_missing( sym , *args )
> @@api[sym] ||= Functor.new { |op, *args|
> api_call("#{sym}.#{op}", *args )
> }
> end
> end

Well, it helps (and that calibre library is quite cool), but: it
fails when there's more than three terms in the method, e.g.:
Flickr.photos.licenses.getInfo({"something" => "blah"})
undefined method `getInfo' for nil:NilClass (NoMethodError)

The general case (flickr.a.b...n), doesn't seem possible to me
without the object returned from #method_missing having the same
behaviour (i.e. implementation of #method_missing) as the initial
object, but with the previous calls as part of the object's state.

matt.


1 Answer

Trans

12/8/2005 3:04:00 AM

0

Hmm.... that does make it trickier b/c when will the chain end? The
only thing I can think of the top of my head is to use an '!' method to
indicate it.

Flickr.test.echo!

Then you can just return the same Functor-like object collecting the
parts along the way until the '!' is hit.

T.