What is the “right” way to iterate through an array in Ruby?

后端 未结 12 1739
盖世英雄少女心
盖世英雄少女心 2020-12-04 04:18

PHP, for all its warts, is pretty good on this count. There\'s no difference between an array and a hash (maybe I\'m naive, but this seems obviously right to me), and to ite

12条回答
  •  渐次进展
    2020-12-04 05:05

    In Ruby 2.1, each_with_index method is removed. Instead you can use each_index

    Example:

    a = [ "a", "b", "c" ]
    a.each_index {|x| print x, " -- " }
    

    produces:

    0 -- 1 -- 2 --
    

提交回复
热议问题