From: David Alan Black Date: 2002-07-12T03:15:41+09:00 Subject: Re: spliter of for...each: looping Hi -- On Fri, 12 Jul 2002, Tom Sawyer wrote: > > arr.collect{|x| "junk#{x}morejunk" }.join(",") > > can you put conditionals in that? > > q = '' > arr.each_with_nindex do |x, n| > q << 'junk' << x > q << 'morejunk' if x == 'more' > q << ',' if n != -1 > end Sure: arr.collect {|x| "junk#{x}#{'more' if x=='more'}" }.join "," That's in the nature of #collect (aka #map): you're basically translating from your original array to a new array of the same length, using what happens in the block (one element at a time) as the translator or transformer. And "what happens in the block" can certainly be conditional on the value of the element(s). By the way, another way to do the all-but-last thing with an array is: arr[0..-2].each {|e| } David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav