Hi --

On Tue, 12 Dec 2006, J2M wrote:

> This is what I finally ended up with - which I think is quite elegant
> :)
>
> class Foo
>    def bar(quantal, *args, &block)
>      if block_given?
>        lambd = lambda(&block)
>        class << lambd
>          @@return = {}.extend(Appenders::Hashs)
>          def method_missing(m, *args, &block)
>            return @@return << { m.to_sym => args_to_attributes(args) }

I'm curious where args_to_attributes is defined.  Is it a top-level
method?

>          end
>        end
>        attributes = lambd.instance_eval(&lambd)
>      end
>      parse quantal, attributes

If that if block hasn't run, attributes is going to be undefined.  Did
you mean to put this line inside the block?

>    end
> end # Foo
>
> What made this sweet for me was turning the &block into a lambda let me
> return the result from the instance_eval to bar where other solutions
> kept them in scope of the &block.

But the block *is* a lambda :-)  That's what the &block argument does:
it captures the code block in an object and assigns a variable to it.

You could probably also get rid of the class variable by using
define_method.  (Not essential, but always a bit of a plus style-wise,
I think.)


David

-- 
Q. What's a good holiday present for the serious Rails developer?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    aka The Ruby book for Rails developers!
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)