each_with_index

You won’t find each_with_index in the ruby Array class documentation. But this handy little method allows you to iterate through an array while also keeping track of which element you’re on.

It works something like this:
my_array.each_with_index do
|a, index| puts "#{a} is located at index #{index}"
end

Sure beats typing
i=0; i++; i<(array.size -1)

2 comments October 15th, 2008


RSS Berkman Gender & Tech

RSS Tweets

Tags

Meta