David Masover
4/26/2009 2:23:00 AM
On Saturday 25 April 2009 03:17:50 Steven Taylor wrote:
> To me, it appears as if the method has to be read in
> conjunction with how the method is actually called in order to know if a
> block is passed or not.
If your question is how to tell whether a method accepts a block, the answer
is, it does. All methods do. They just might not do anything with it.
If your question is whether a method does anything with the block, there are
three ways to tell.
The first is documentation! On larger projects, like Rails and Merb, the public
API is at least very well documented. Even reading just the HTML pages, you'll
see mention of whether it accepts a block, or an options hash, what options
are significant in that hash, possibly what exceptions it throws, and some
actual usage examples.
The second way is if it has a &foo argument at the end -- for example:
def foo arg1, arg2, &block
Of course, technically, the method could ignore that, so look for that
variable (in this case, 'block') being used.
If all else fails, read the method looking for any 'yield' call. I don't
believe 'yield' will ever do anything but call the current block, inside the
appropriate method.
If your question is how to tell, programmatically, whether the current method
has been passed a block, just call 'block_given?'