From: Brian Candler Date: 2009-01-16T22:22:18+09:00 Subject: Re: yield example baffling - need help Tom Cloyd wrote: > How can that yield do anything? How does it know about the block? It's > not passed in. The iteration appears to be happening IN the method, > using a block it cannot possibly see. Ouch! After some minutes, I'm not > getting this at all. Maybe it's clearer if you rewrite myeach to show explicitly the block being passed in, and being called. def myeach(myarray, &block) # <<< iter = 0 while (iter < myarray.length): block.call(myarray[iter]) # <<< iter += 1 end end Even if there is no &foo in the argument list, you can still pass a block when you call the method. The method can call it anonymously using 'yield', and test for its presence using 'block_given?' (*) BTW, Proc#[] is an alias for Proc#call, so this gives you three choices: block.call(args) # only if block captured into a variable block[args] # ditto yield args HTH, Brian. (*) It's occasionally a source of head-scratching when you pass a block to a method which doesn't expect one, and so it's silently ignored. Ruby can't and doesn't warn you of this. -- Posted via http://www.ruby-forum.com/.