what are Ranges : Range Creation « Range « Ruby






what are Ranges


Ruby has ranges, with range operators and a Range class. 
Ranges are intervals with a start value and an end value, separated by a range operator. 
There are two range operators, .. (two dots) and ... (three dots). 
The range operator .. means an inclusive range of numbers. 
1..10 means a range of numbers from 1 to 10, including 10 (1, 2, 3, 4, 5, 6, 7, 9, 10). 
The range operator ... means an exclusive range of numbers that exclude the last in the series; 
in other words, 1...10 means a range of numbers from 1 to 9, as the 10 at the end of the range is excluded (1, 2, 3, 4, 5, 6, 7, 9).

The === method determines if a value is a member of, or included in, a range

(1..25) === 14 # => true, in range
(1..25) === 26 # => false, out of range
(1...25) === 25 # => false, out of range if ... used

 








Related examples in the same category

1.With the Range class, you can also create a range
2.To create a range, use the .. operator. For example, here's how you might create the range 1 to 4:
3.use the ... operator, which is the same thing except that the final item in the range is omitted
4.Reference to_a from a range
5.Reference to_a from a range in an excluded range
6.Reference to_a from a letter-based range
7.You have to create ranges in ascending sequence
8.Variable based range