From: "David A. Black" Date: 2002-12-03T23:59:40+09:00 Subject: Re: Conditional block operations Hi -- On Tue, 3 Dec 2002, Roman Rytov wrote: > Anotehr question is about COLLECT; > > myarray.collect {|x| x + ", "} > > I wanna execute only over specific emelents (let's say only odd ones or > all but last). > > I know how to do both of them in a couple of lines but RUBY impresses my > by its brevity. Will it do it in this case too? You need to get your shift key fixed :-) There's no RUBY, just Ruby and ruby. As for collect'ing selectively, if the elements you *don't* want are at a known distance from either end of the array, you can do: myarray[0..-2].collect {...} # up to 2nd-to-last element etc. Otherwise I think you have to do some kind of pre-filtering. If we had map_with_indices, you could do: myarray.map_with_indices {|e,i| e + ", " if i % 2 == 0} .compact but we don't. (Hint, hint.) However, you could do: (0...myarray.size).map {|i| myarray[i] + ", " if i % 2 == 0} .compact That's one-line-ish, anyway :-) David -- David Alan Black exiled to ISP shell account due to recalcitrant ISP :-(