在这种情况下,更多惯用的[可论证]方式是使用Integer#upto:
1.upto(A) { |n| puts n }
Run Code Online (Sandbox Code Playgroud)
此外,step(1)是默认值,您可以简单地迭代范围本身:
(1..A).each { |n| puts n }
Run Code Online (Sandbox Code Playgroud)
或者,甚至使用Integer#times:
A.times { |n| puts n + 1 }
Run Code Online (Sandbox Code Playgroud)
请注意,这Integer#times开始计数,0因此+ 1是必需的.
NB也请注意由下@Stefan了非常宝贵的意见.
| 归档时间: |
|
| 查看次数: |
1936 次 |
| 最近记录: |