开发者

incremental array in ruby, 0..40, [10, 20, 30, 40]

How can I offset this array so it only outputs every ten numbers开发者_运维知识库?

(0...40)
[10,20,30,40]


Use the step method for this:

10.step(40,10)

The first argument is the number you want to count up to, with the second argument being the "steps" that you take to get there.


(0..40).step(10) {|num| puts num }  # => 0 10 20 30 40

This will take (0..40) and increment by 10 each time.


(0..40).select { |i| i != 0 && i % 10 == 0 }

is one way.


or even the more efficient version ;)

(1..4).collect { |n| n*10 }

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜