问题
I'm iterating through an array in ruby with each. Is there an easy way to get the iteration number or array index without going back to a for loop?
回答1:
Ah, got it.
each_with_index
woo!
edit: whoops!
回答2:
For completeness' sake.
Thing.each_with_index {|obj, index_num| puts [obj, index_num].join(" has an index of ")}
来源:https://stackoverflow.com/questions/706115/how-to-get-the-array-index-or-iteration-number-with-an-each-iterator