Ranges as Sequences
suggest changeThe most important use of ranges is to express a sequence
Syntax:
(begin..end) => this construct will include end value
(begin...end) => this construct will exclude end value
or
Range.new(begin,end,exclude_end) => exclude_end is by default false
For these sequences, end must be greater than begin. Otherwise enumerating the range yields nothing. A descending numeric range is still a range, and it is empty when you call to_a.
Examples:
(10..1).to_a #=> []
(1...3).to_a #=> [1, 2]
(-6..-1).to_a #=> [-6, -5, -4, -3, -2, -1]
('a'..'e').to_a #=> ["a", "b", "c", "d", "e"]
('a'...'e').to_a #=> ["a", "b", "c", "d"]
Range.new(1,3).to_a #=> [1, 2, 3]
Range.new(1,3,true).to_a#=> [1, 2]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents