[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?

Daniel Sheppard

12/8/2005 6:35:00 AM

Considering that there seems to be at least one mandatory argument
(api_key), you could do:

require 'calibre/functor'

functor_factory = lambda { |*symbols|
Functor.new { |op, *args|
new_sym = symbols+[op]
if args.empty?
functor_factory[*new_sym]
else
puts "call:
http://flickr.com/services/rest/?met...{new_sym}'"
puts "with parameters:"
p args
end
}
}
Flickr = functor_factory[]

Flickr.test.echo.momma("api_key" => "something long", "foo" => "bar")

> -----Original Message-----
> From: Trans [mailto:transfire@gmail.com]
> Sent: Thursday, 8 December 2005 2:08 PM
> To: ruby-talk ML
> Subject: Re: Are my metaprogramming underpants showing?
>
> 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.
>
>
>
>
#####################################################################################
This email has been scanned by MailMarshal, an email content filter.
#####################################################################################


3 Answers

Trans

12/8/2005 7:10:00 AM

0

Daniel...

WAY COOL!

T.

Matthew Smillie

12/8/2005 4:12:00 PM

0

On Dec 8, 2005, at 6:35, Daniel Sheppard wrote:


> Considering that there seems to be at least one mandatory argument
> (api_key), you could do:
>
> require 'calibre/functor'
>
> functor_factory = lambda { |*symbols|
> Functor.new { |op, *args|
> new_sym = symbols+[op]
> # if args.empty?...
> }
> }
> Flickr = functor_factory[]
>
> Flickr.test.echo.momma("api_key" => "something long", "foo" => "bar")
>

Maybe this makes me sound thick, but then again, enlightenment is the
reason I'm posting.

Given that the code below is functionally equivalent, what's
beneficial about using a lambda expression? It strikes me as a
requirement imposed by using the Functor class more than anything.

class Flickr
def initialize (method = ['flickr'])
@method = method
end

def method_missing(method_id, *params)
new_method = @method + [method_id]
if params.empty?
self.class.new(new_method)
else
puts "call: http://flickr.com/services/res...
{new_method.join('.')}"
p params
end
end
end

flickr = Flickr.new
flickr.test.echo.momma("api_key" => "something long", "foo" => "bar")

thanks again for the input,
matthew smillie.



----
Matthew Smillie <M.B.Smillie@sms.ed.ac.uk>
Institute for Communicating and Collaborative Systems
University of Edinburgh




Trans

12/8/2005 5:25:00 PM

0

> Given that the code below is functionally equivalent, what's
> beneficial about using a lambda expression?

I don't think there is neccessarily. Functor's just the completely
generic form of this. Making a specifec Functor-like class as you have
here with this Flickr class is ceratinly the way to go.

T.