From: Trans Date: 2005-10-14T23:36:55+09:00 Subject: Re: Default argument values for blocks Ara.T.Howard wrote: > On Fri, 14 Oct 2005, Christian Neukirchen wrote: > > > "David A. Black" writes: > > > >> Hi -- > >> > >> On Fri, 14 Oct 2005, Gyoung-Yoon Noh wrote: > >> > >>> +1 > >>> > >>> collection.each : (foo="bar") { puts foo } > >>> > >>> It looks like 'when condition : expression' branch in > >>> 'case' statement. > >> > >> But why would you want a method call and argument list and block to > >> look like when condition : expression ? There's no "then" meaning to > >> it. > > > > Read the : as "do". When x is one, do... For every item of the > > collection, do... > > > > I think the :(){} stuff is the nicest looking approach as of now, but > > I wouldn't use it when ordinary {||} is enough. > > it all seems like a __lot__ of coding for someone when one can simply > > harp:~ > cat a.rb > collection = [ 4, nil, 2 ] > collection.each{|foo| foo ||= "bar"; puts foo } > > harp:~ > ruby a.rb > 4 > bar > 2 > > by my reckoning that's only a few xtra chars > > if one wanted to do that alot you can just > > harp:~ > cat a.rb > class Array > def merge *mask > zip(mask.flatten).map{|v| v.first or v.last} > end > def merge! *mask > replace merge(*mask) > end > end > > table = > [ > [nil, 4, 2], > [4, nil, 2], > [4, 2, nil], > ] > > table.each do |row| > row.merge! defaults = %w(a b c) > p row > end > > harp:~ > ruby a.rb > ["a", 4, 2] > [4, "b", 2] > [4, 2, "c"] > > > imho > > table.each do |row|; row.merge! %w(a b c) > p row > end > > is very nearly as clear and as concise as > > table.each do |x = 'a', y = 'b', z = 'c'| > row = [x, y, z] > p row > end > > and this extends to multi-parm blocks just as easily > > assoc_array.each do |k = 'no key', v = 'no val'| > p [k, v] > end > > assoc_array.each do |kv|; kv.merge! 'no key', 'no val' > p kv > end > > i guess i'm just thinking block parameters, and method parameters, are always > lists (possibly empty) and that a nifty new list operator could solve this > with even saying yac... I don't think we should be relegated to these kinds of cleverness. It is desireable to have lamda and def have the same basic semantics. OTOH, It would seem in a manner that you are suggesting perhaps we go the other way and get rid of default parameters altogeter and use more explict means, eg. psuedo-code: def foo(?a) a = 1 if a.nack? end Don;t dismiss the idea so readily either. It does have merit, since default parameters are not at all fullproof. No doubt you, like myself often HAVE to do something like the above in many cases to prevent certain "bad" arguments getting thru. Default parameters can actually encourage poor management of parameters. So maybe this is a good idea. And then the whole need for ->(){} would just go away. T.