当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


Ruby Integer.upto用法及代码示例

本文简要介绍ruby语言中 Integer.upto 的用法。

用法

upto(limit) {|i| ... } → self
upto(limit) → enumerator

使用从 selflimit 的每个整数值调用给定块;返回 self

a = []
5.upto(10) {|i| a << i }              # => 5
a                                     # => [5, 6, 7, 8, 9, 10]
a = []
-5.upto(0) {|i| a << i }              # => -5
a                                     # => [-5, -4, -3, -2, -1, 0]
5.upto(4) {|i| fail 'Cannot happen' } # => 5

如果没有给出块,则返回 Enumerator

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Integer.upto。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。