From: Guten Date: 2010-12-31T22:12:44+09:00 Subject: Re: a common idiom --20cf300258ccf7b7a70498b48dff Content-Type: text/plain; charset=ISO-8859-1 enumerator.each_with_object [] do |value, memo| memo << value unless value.nil? end On Fri, Dec 31, 2010 at 9:11 PM, Guten wrote: > enumerator.each_with_object [] do |value, memo| > memo << value if value.nil? > end > > > > On Fri, Dec 31, 2010 at 6:50 PM, Michel Demazure wrote: > >> Michael Fellinger wrote in post #971622: >> > compact >> Yes ! I knew there was a method, but forgot the name, browsed thru >> Flanagan-Matz, and did not find it. >> >> > results = [] >> > some.each do |value| >> > results << value.operation if value.condition? >> > end >> >> Yes, returning results, but I try to avoid this extra variable. I >> dislike (question of taste) the pattern >> def ... >> res = [] >> ... >> res << >> ... >> res >> end >> >> Another way is using tap >> >> Array.new.tap do |res| >> ... >> end >> >> A bit snob, no ? >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > --20cf300258ccf7b7a70498b48dff--