Range
- : Two dots denote an inclusive range, including
x
andy
and all values in between (in mathematics:[x, y]
) . x...y
: Three dots denote an exclusive range, includingx
and all values up to but not includingy
(in mathematics:[x, y)
).
!!! note
Range literals are often wrapped in parentheses, for example if it is meant to be used as the receiver of a call. 0..5.to_a
without parentheses would be semantically equivalent to 0..(5.to_a)
because method calls and other operators have higher precedence than the range literal.
The literal is semantically equivalent to the explicit constructor Range.new(x, y)
and x...y
to Range.new(x, y, true)
.
Ranges that begin with nil
are called begin-less ranges, while ranges that end with nil
are called end-less ranges. In the literal notation, nil
can be omitted: x..
is an end-less range starting from , and ..x
is an begin-less range ending at x
.
numbers.select(6..) # => [10, 8]
numbers.select(..6) # => [1, 3, 4, 5]
numbers[2..] = [3, 4, 5, 8]
numbers[..2] = [1, 10, 3]