From: "David A. Black" Date: 2005-11-25T22:23:03+09:00 Subject: Re: Faster way to pick the every other array member? Hi -- On Fri, 25 Nov 2005, Damphyr wrote: > we have [1,2,3,4,5,6,7] and want [2,4,6] > > Is there a way other than > > fields=[1,2,3,4,5,6,7] > fields.collect!{|f| > f if fields.index(f)%2==1 > }.compact! > > And by another way I mean something shorter and more beautiful :). Here's one possibility: irb(main):016:0> a = [1,2,3,4,5,6,7] => [1, 2, 3, 4, 5, 6, 7] irb(main):017:0> res = [] => [] irb(main):018:0> (1...a.size).step(2) {|n| res << a[n] } => 1...7 irb(main):019:0> res => [2, 4, 6] David -- David A. Black dblack@wobblini.net