How to iterate over an array skipping every second element?
6 answers
You can also try:
> arr = Array('a'..'z')
#=> ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
> arr.select.each_with_index{|k,i| k if i.even?}
#=> ["a", "c", "e", "g", "i", "k", "m", "o", "q", "s", "u", "w", "y"]
I like step
, and slice
a method that has already been said, so it's just a different approach .;)
+2
source to share