Hi -- On Mon, 21 Nov 2005, David Harrigan wrote: > Hi, > > I'm getting my head around blocks, closures etc in Ruby - I'm from a > Java background. Now, I'm reading the book "Programming Ruby - The > Pragmatic Programmer's Guide" and in the section titled "Blocks for > Transactions" there is an example of using a block for a file > operation. However, examining this code, and especially with a view of > hiding detail from a receiver, I find myself in some puzzlement, > because of this reason: > > Another object simply can't call > File.openAndProcess("another_testfile", "r") WITHOUT knowing that one > also has to write a block because inside openAndProcess there is a > "yield". This means that the method openAndProcess isn't that > invisible, the callee must have some detailed knowledge about the > method (i.e., the fact that it calls a yield) so that they can write a > block. > > Am I missing something here? You make it sound like yielding to a block is a method's dirty secret :-) The caller is *supposed* to know how to call the method -- so if your method takes a block, you would say so in the documentation. For example, here's part of the ri output for Array#each: ------------------------------------------------------------- Array#each array.each {|item| block } -> array ---------------------------------------------------------------- Calls _block_ once for each element in _self_, passing that element as a parameter. David -- David A. Black dblack / wobblini.net