How to create this array in Ruby?

来源:互联网 发布:小啰啰崩没哏网络直播 编辑:程序博客网 时间:2024/05/21 11:20

I there a smarter way to define an array like this in Ruby?

array = [5, 15, 25, 35, 45, 55, 65, 75]

Thanks for any help.

Use Range#step:

Range.new(5, 75).step(10).to_a# => [5, 15, 25, 35, 45, 55, 65, 75][*Range.new(5, 75).step(10)]# => [5, 15, 25, 35, 45, 55, 65, 75][*(5..75).step(10)]  # (5..75) == Range.new(5, 75)# => [5, 15, 25, 35, 45, 55, 65, 75]
5.step(75, 10).to_a #=> [5, 15, 25, 35, 45, 55, 65, 75]

0 0
原创粉丝点击