关于红宝石范围? [英] about ruby range?

查看:66
本文介绍了关于红宝石范围?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像这样

range = (0..10)

我如何获得这样的数字:

how can I get number like this:

0 5 10 

每次加五,但少于10

plus five every time but less than 10

如果范围=(0..20),那么我应该得到这个:

if range = (0..20) then i should get this:

0 5 10 15 20


推荐答案

尝试使用 .step() 进行给定步骤。

Try using .step() to go through at a given step.

(0..20).step(5) do |n|
    print n,' '
end

给出...

0 5 10 15 20

如dominikh所提到的,您可以在末尾添加 .to_a 以获得数字列表的可存储形式:(0 ..20).step(5).to_a

As mentioned by dominikh, you can add .to_a on the end to get a storable form of the list of numbers: (0..20).step(5).to_a

这篇关于关于红宝石范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆