From: Mark Hubbart Date: 2004-08-13T02:29:55+09:00 Subject: Re: [proto-rcr] Blocks: default arguments and method signatures Since most of the problems in here have been hashed over already by those more competent than me, I'll just mention one thing that caught my eye: On Aug 12, 2004, at 4:26 AM, Sam McCall wrote: > I propose &block could take a default argument, probably of the form > &block={|x| foo}, but I could live with &block=proc {|x| foo}. > block_given? would return *false* if the default value was used (I'm > flexible on this bit). > The default block would be scoped *inside* the method. > That example would become: > > def transform_values(array) &block={|val,newval| out<< newval} > out=[] unless block_given? > array.each { |value| > #calculations... > yield value,newvalue > } > end To me, that code should make a new variable, named 'block'. How about this: def transform_values(array) {|val,newval| (out||=[])<< newval} out=[] unless block_given? array.each { |value| #calculations... yield value,newvalue } end (note that or-equaling 'out' solves the forward reference problem, at the cost of some visual cleanliness) Basically, an unassigned block at the beginning of a method declaration would be taken as the yield block. Another possibility would be to give the block it's own binding, as if it was defined in an intermediate method. That way, you wouldn't ever have to worry about namespace clashes. cheers, Mark