1..5 => [0, 1, 2, 3, 4, 5]
1...5 => [0, 1, 2, 3, 4]
'a'..'d' => ['a', 'b', 'c', 'd']
'a'...'d' => ['a', 'b', 'c']
(1..10) === 10 => true
(1...10) === 10 => false
(1..10) === 15 => false
while gets # prints lines starting at 'start' and ending at 'end'
print if /start/../end/
end
# represent a string of 'x's as a Range
class Xs
include Comparable
def initialize(n)
@length = n
end
def inspect
'x' * @length
end
# override the following for range utility
def succ
Xs.new(@length + 1)
end
def <=>(other)
@length <=> other.length
end
end
r = Xs.new(3)..Xs.new(6) #=> xxx..xxxxxx
r.to_a #=> [xxx, xxxx, xxxxx, xxxxxx]
r.member?(Xs.new(5)) #=> true
# Ranges can use === comparisons, thus can use case statements
case 12
when 1..10 then puts "less than 10"
when 10..20 then puts "between 10 and 20"
when 20..30 then puts "greater than 20"
end
# outputs => between 10 and 20