From: dblack@... Date: 2006-01-02T22:16:27+09:00 Subject: Re: About blocks Hi -- On Mon, 2 Jan 2006, Surgeon wrote: > Hi, > > (This question might be answered previously. If so, I apologize.) > > Can we *ALWAYS* use plain methods instead of blocks? Yes, sometimes > using a block may be obviously better than using a method but I mean, > are there any situations in which the *ONLY* solution is to use a block > rather than a method? May someone be forced to use only and only a > block? I ask this because I am not very intelligent and I am not sure > that I will thoroughly understand the "block" concept. You don't really use methods instead of blocks; you use methods -- that is, you call methods -- with or without providing a code block for a given method call. The block is part of the method call syntax, along with the method name and the argument list. Whether or not it's mandatory is up to the method (as with arguments). For example: def x end def y yield end def z yield if block_given? end x # OK -- no block expected or given x { puts "code block" } # block OK but ignored y # error, because no block to yield to y { puts "code block" } # OK z # OK -- no yield will be attempted z { puts "code block" } # also OK, and block will execute David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black