Looping through an array with step

后端 未结 7 1044
余生分开走
余生分开走 2020-12-05 23:24

I want to look at every n-th elements in an array. In C++, I\'d do this:

for(int x = 0; x

        
相关标签:
7条回答
  • 2020-12-06 00:21

    What about:

    > [1, 2, 3, 4, 5, 6, 7].select.each_with_index { |_,i| i % 2 == 0 }
    => [1, 3, 5, 7]
    

    Chaining of iterators is very useful.

    0 讨论(0)
提交回复
热议问题