【发布时间】:2014-07-10 04:19:58
【问题描述】:
Ruby 文档显示Range#step 接受一个参数。它似乎用于以传递给step 的数字为增量迭代一个范围。
(0..100).step(5) { |x| puts x }
应该产生:
0
5
10
15
...
在埃拉托色尼筛的例子中,人们通过Range#step 似乎是两个参数,如here 所示:
(primes[index] * 2).step(primes.last, primes[index]) do
这里发生了什么?当你传递step 两个参数时会发生什么?当我用类似的东西测试它时:
(0..100).step(5,10) { |x| puts x }
我明白了:
ArgumentError:参数数量错误(2 代表 0..1)
【问题讨论】:
标签: ruby