From: gabriele renzi Date: 2004-12-04T20:02:40+09:00 Subject: Re: Using yield Joe Van Dyk ha scritto: > Sam Stephenson wrote: > >>On Sat, 4 Dec 2004 09:32:39 +0900, Joe Van Dyk >> wrote: >> >>>When do you use the 'yield' statement in code? >> >>When you want to write methods that take blocks. > > > Yes. What types of methods generally should take blocks? > My little list: [1] those when behaviour can be further specified. i.e. #sort and #sort_by can get a block to specify how to sort an object [2] those where a resource could be freed i.e. constructors like File.new or Socket.new get a block so that the undelying resource would be freed when the block ends instead of waiting for the gc. [3] those where one would like to lazyly use/reuse a snippet of code given from the user i.e. set_trace_func There are surely others, the best way to get the felling is probably looking at existing ruby code.