Ruby each_with_index offset

前端 未结 10 1035
长发绾君心
长发绾君心 2020-12-12 23:32

Can I define the offset of the index in the each_with_index loop iterator? My straight forward attempt failed:

some_array.each_with_index{|item, index = 1| s         


        
10条回答
  •  时光说笑
    2020-12-13 00:04

    Ariel is right. This is the best way to handle this, and it's not that bad

    ary.each_with_index do |a, i|
      puts i + 1
      #other code
    end
    

    That is perfectly acceptable, and better than most of the solutions I've seen for this. I always thought this was what #inject was for...oh well.

提交回复
热议问题