Looping through an array with step

后端 未结 7 1070
余生分开走
余生分开走 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-05 23:58

    class Array
    def step(interval, &block)
        ((interval -1)...self.length).step(interval) do |value|
            block.call(self[value])
        end
    end
    end
    

    You could add the method to the class Array

提交回复
热议问题