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
An easy way to remember which one is inclusive and which one is exclusive it to think of the extra dot as if it pushes y further away, thus leaving it outside of the range.
The begin and end values do not necessarily need to be of the same type: true..1
is a valid range, although pretty useless since Enumerable
methods won’t work with incompatible types. They need at least to be comparable.
numbers.select(6..) # => [10, 8]
numbers.select(..6) # => [1, 3, 4, 5]
numbers[2..] = [3, 4, 5, 8]
numbers[..2] = [1, 10, 3]
A range that is both begin-less and end-less is valid and can be expressed as ..
or but it’s typically not very useful.