ruby 范围生成中“..”(双点)和“...”(三点)之间的区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9690801/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between '..' (double-dot) and '...' (triple-dot) in range generation?
提问by juil
I've just started learning Ruby and Ruby on Rails and came across validation code that uses ranges:
我刚刚开始学习 Ruby 和 Ruby on Rails,并遇到了使用范围的验证代码:
validates_inclusion_of :age, :in => 21..99
validates_exclusion_of :age, :in => 0...21, :message => "Sorry, you must be over 21"
At first I thought the difference was in the inclusion of endpoints, but in the API docs I looked into, it didn't seem to matter whether it was ..or ...: it always included the endpoints.
起初我认为区别在于包含端点,但在我查看的 API 文档中,它是..或似乎并不重要...:它总是包含端点。
However, I did some testing in irb and it seemed to indicate that ..includes both endpoints, while ...only included the lower bound but not the upper one. Is this correct?
但是,我在 irb 中做了一些测试,它似乎表明..包括两个端点,而...只包括下限而不包括上限。这样对吗?
回答by Andrew Marshall
The documentation for Range?says this:
Ranges constructed using
..run from the beginning to the end inclusively. Those created using...exclude the end value.
使用
..从头到尾运行构建的范围。那些使用创建的...排除最终值。
So a..bis like a <= x <= b, whereas a...bis like a <= x < b.
所以a..b就像a <= x <= b,而a...b就像a <= x < b。
Note that, while to_aon a Range of integers gives a collection of integers, a Range is nota set of values, but simply a pair of start/end values:
请注意,虽然to_a在整数范围上给出了整数集合,但范围不是一组值,而只是一对开始/结束值:
(1..5).include?(5) #=> true
(1...5).include?(5) #=> false
(1..4).include?(4.1) #=> false
(1...5).include?(4.1) #=> true
(1..4).to_a == (1...5).to_a #=> true
(1..4) == (1...5) #=> false
?The docs used to not include this, instead requiring reading the Pickaxe's section on Ranges. Thanks to @MarkAmery (see below) for noting this update.
? 文档过去不包括此内容,而是需要阅读Pickaxe 关于 Ranges 的部分。感谢@MarkAmery(见下文)注意到此更新。
回答by Chris Heald
That is correct.
那是正确的。
1.9.3p0 :005 > (1...10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
1.9.3p0 :006 > (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The triple-dot syntax is less common, but is nicer than (1..10-1).to_a
三点语法不太常见,但比 (1..10-1).to_a
回答by Mark Amery
The API docs now describe this behaviour:
API 文档现在描述了这种行为:
Ranges constructed using
..run from the beginning to the end inclusively. Those created using...exclude the end value.
使用
..从头到尾运行构建的范围。那些使用创建的...排除最终值。
In other words:
换句话说:
2.1.3 :001 > ('a'...'d').to_a
=> ["a", "b", "c"]
2.1.3 :002 > ('a'..'d').to_a
=> ["a", "b", "c", "d"]
回答by Dennis
a...bexcludesthe end value, while a..bincludesthe end value.
a...b不包括最终值,而a..b包括最终值。
When working with integers, a...bbehaves as a..b-1.
处理整数时,a...b表现为a..b-1.
>> (-1...3).to_a
=> [-1, 0, 1, 2]
>> (-1..2).to_a
=> [-1, 0, 1, 2]
>> (-1..2).to_a == (-1...3).to_a
=> true
But really the ranges differ on a real number line.
但实际上范围在实数轴上有所不同。
>> (-1..2) == (-1...3)
=> false
You can see this when incrementing in fractional steps.
您可以在以小数步长递增时看到这一点。
>> (-1..2).step(0.5).to_a
=> [-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0]
>> (-1...3).step(0.5).to_a
=> [-1.0, -0.5, 0.0, 0.5, 1.0, 1.5, 2.0, 2.5]
回答by daniel
.. and ... denote a range.
.. 和 ... 表示一个范围。
Just see it in irb:
只需在 irb 中查看:
ruby-1.9.2-p290 :032 > (1...2).each do puts "p" end
p
=> 1...2
ruby-1.9.2-p290 :033 > (1..2).each do puts "p" end
p
p

