From: "David A. Black" Date: 2008-05-11T03:08:48+09:00 Subject: Re: Delete every other value in an array Hi -- On Sun, 11 May 2008, 7stud -- wrote: > Tim Conner wrote: >> What is the best way to delete every other value in a ruby array? >> e.g. >> %w(a b c d e f g h i j k) >> >> becomes => [a c e g i k] >> >> thanks > > > letters = ("a".."z").to_a > last_index = letters.length - 1 > > results = [] > > 0.step(last_index, 2) do |i| > results << letters[i] > end > > letters = results > p letters In Ruby 1.9 you can do: letters.values_at(*0.step(letters.size-1,2)) using the enumerator returned from step called without a block. David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates!