From: Kirk Haines Date: 2005-08-08T04:30:34+09:00 Subject: Re: Adding yet another Array.new form On Sunday 07 August 2005 10:50 am, David A. Black wrote: > >> def a > >> returning ary = [] do > >> ... construct ary by adding elements to it ... > >> end > >> end > > [...] > > > I'd rather vote in favor of including #returning to the Ruby core. > > It is a very helpful method to DRY. > > It feels a bit side-effect-dependent-ish, as written. It might be > nicer with a more traditional yield/block param syntax: > > returning [] do |ary| ... end > > That shows more of the separation between the fact that you're > returning an array, and the fact that you're assigning the name ary to > that array for use inside the block. It should work either way. I haven't looked at the one in Rails, but mine: module Kernel def returning(*vals) yield *vals end end def a returning tmp = [] do tmp << 1; tmp << 2; tmp << 3 end end def b returning [] do |ary| ary << 1; ary << 2; ary << 3 end end irb(main):013:0> a => [1, 2, 3] irb(main):014:0> b => [1, 2, 3] And I agree with you. I like how the usage shown in b looks, myself, at least in the places where I use this construction. Kirk Haines