【问题标题】:How to get the array index or iteration number with an each iterator?如何使用每个迭代器获取数组索引或迭代号?
【发布时间】:2009-04-01 15:43:11
【问题描述】:

我正在遍历每个 ruby​​ 中的数组。有没有一种简单的方法可以在不返回 for 循环的情况下获取迭代次数或数组索引?

【问题讨论】:

    标签: ruby indexing iterator


    【解决方案1】:

    啊,明白了。

    each_with_index
    

    哇!

    编辑:哎呀!

    【讨论】:

      【解决方案2】:

      为了完整起见。

      Thing.each_with_index {|obj, index_num| puts [obj, index_num].join(" has an index of ")}
      

      【讨论】:

        猜你喜欢
        • 2013-07-05
        • 2020-10-13
        • 2014-04-24
        • 1970-01-01
        • 1970-01-01
        • 2010-09-13
        • 1970-01-01
        • 2021-04-17
        • 2011-05-12
        相关资源
        最近更新 更多