How to get the array index or iteration number with an each iterator?

这一生的挚爱 提交于 2019-12-18 04:17:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!