From: dblack@... Date: 2006-01-02T22:36:42+09:00 Subject: Re: About blocks Hi -- On Mon, 2 Jan 2006, Robert Klemme wrote: > 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 need blocks everywhere you need an anonymous callback function such as > with iterations: > > an_array.each {|e| ... } > > And you need lambdas (i.e. blocks converted to an object) if you need to > store the thing in a variable: > > class SortedArray < Array > attr_accessor :comparator > def each > sort!(&comparator) > super(&b) I think you need either: def each(&b) or just: super # this seems to propagate the block, at least in 1.8.3 # (I think this might have changed, but not sure.) David -- David A. Black dblack@wobblini.net "Ruby for Rails", from Manning Publications, coming April 2006! http://www.manning.com/books/black