Hello --


On Mon, 5 Nov 2001, Bob Sidebotham wrote:

> David Alan Black wrote:
> >
> > Hello --
> >
> > On Sun, 4 Nov 2001, Bob Sidebotham wrote:
> David Alan Black wrote:
> > The important thing to remember, though, is that you can associate a
> > non-argument block with any method call.  So if you send a block as a
> > regular argument, you can still also associate a block:
> >
> >   def thing(a,b,c)
> >     puts yield c.call(a, b)
> >   end
> >
> >   thing(1,2, proc { |x,y| x + y }) { |n| "result is #{n}" }
> >
> >   # =>  result is 3
>
> Why is this important? This still seems to be sugar for:
>
>   def thing(a,b,c,d)
>     puts d.call(c.call(a, b))
>   end
>
>   thing(1,2, proc { |x,y| x + y }, proc { |n| "result is #{n}" })
>
>   # =>  result is 3
>
> Does the first syntax somehow express the _intent_ of the code better?

I just meant that this:

   thing(args) { # block }

isn't exactly the same as:

   b = proc { # block }
   thing(args, b)

because the latter call to thing can still have a block associated
with it.  Unless you do:

   thing(args, &b)

in which case &b counts as the associated block, not a regular
argument.  (But has to be in the argument list, as Kent has just
pointed out :-)

> Help me! I really have not seen the light yet.

Well, yield(ing control to a block) is a little different from
call(ing a proc).  There's probably more light to be had from others,
but I'll just point out this:

  def thing(&b)
    yield 123      # 123
    b.call(123)    # wrong # of args
  end

  thing { |a,b| puts a }


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav