Forum: Ruby Re: Are my metaprogramming underpants showing?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1c1e3bdfe006a22214102fcd6434a012?d=identicon&s=25 daniels (Guest)
on 2005-12-08 07:36
(Received via mailing list)
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/?method='#{new_sym...
            puts "with parameters:"
            p args
        end
    }
}
Flickr = functor_factory[]

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

>
>   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.
#####################################################################################
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-12-08 08:13
(Received via mailing list)
Daniel...

  WAY COOL!

T.
31af45939fec7e3c4ed8a798c0bd9b1a?d=identicon&s=25 M.B.Smillie (Guest)
on 2005-12-08 17:15
(Received via mailing list)
On Dec 8, 2005, at 6:35, Daniel Sheppard wrote:


> }
> 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/rest/method=#
{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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 transfire (Guest)
on 2005-12-08 18:29
(Received via mailing list)
> 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.
This topic is locked and can not be replied to.