Two Types of Ranges in Ruby
Today I learned there are two ways to construct a
range in ruby. Using two dots ..
creates a range including the start and end values.
(2..5).include?(2) # => true
(2..5).include?(5) # => true
Using three dots ...
creates a range including the start value, but not the
end value.
(2...5).include?(2) # => true
(2...5).include?(5) # => false
So if we think of them in terms of
intervals, (a..b)
is
a closed interval ([a, b]
), and (a...b)
is a right half-open interval ([a, b)
).