each_with_index map.
arr = [1, 'foo', 'bar', 'baz', 9]
arr.each_with_index.map { |i,k| i if k.odd? }.compact
values_at
arr.values_at(*(1..arr.size).step(2)).compact
#=> ["foo", "baz"]
and for even indices
arr.values_at(*(0..arr.size).step(2))
#=> [1, "bar", 9]
(I do not consider it reasonable to do this :-))
source
share