each_with_index
October 15th, 2008
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)
Entry Filed under: Ruby on Rails
2 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed
1. mikhailov | October 21st, 2008 at 12:26 am
Did you participate at Rails Rumble?
2. lianaleahy | October 21st, 2008 at 9:58 am
Sadly, no. I have a 7 month old at home that makes extra curricular activities challenging. But I am planning to attend the Voices That Matter conference.