Ruby Language Ranges as Sequences


Example

The 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

Most important end value must be greater the begin, otherwise it will return nothing.

Examples:

(10..1).to_a            #=> []
(1...3)                 #=> [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]