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


Pages

RSS Tweets

RSS Berkman Gender & Tech

Links

Tags

Meta