Jeff Davis <jdavis-list / empires.org> wrote:
> I know I can define a method like:
> 
> def foo(&p)
>     p.call
> end

You can also use "yield" here instead of p.call.

> or:
> 
> def foo
>     yield
> end
> 
> First, what's the difference between those two? I can pass a block to 
> either.

The only difference is that in the second case the block is anonymous.
In the first case, it has a name and so you can pass it around if you
want (e.g. recursively).

> Also, how do I make the block optional? I would like to make a method 
> that performs it's task as usual, but you could also pass it a block 
> that it can use.

Use a "block_given?" test in your method:

def foo(&p)
   if block_given?
      yield
   else
      p "foo"
   end
end

> And also, can you pass more than one block to the same method? Is it 
> only the last argument?

No, but you can pass more than one proc objects (functions) to the
method.  Yes, only the last one can be the official "block".

(I wrote up to here before I saw Florian's answer :)

Cheers,
Navin.