Ranges
The first and perhaps most natural use of ranges is to express a sequence. Sequences have a start point, an end point, and a way to produce successive values in the sequence. In Ruby, these sequences are created using the ". ." and ". . ." range operators. The two dot form creates an inclusive range, and the three-dot form creates a range that excludes the specified high value. In Ruby ranges are not represented internally as lists: the sequence 1..100000 is held as a Range object containing references to two Fixnum objects. Refer program p021ranges.rb. If you need to, you can convert a range to a list using the to_a method.
(1..10).to_a -> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Ranges implement methods that let you iterate over them and test their contents in a variety of ways.
| digits = 0..9 digits.include?(5) -> true digits.min -> 0 digits.max -> 9 digits.reject {|i| i < 5 } -> [5, 6, 7, 8, 9] |
Another use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the range. We do this using ===, the case equality operator.
| (1..10) === 5 -> true (1..10) === 15 -> false (1..10) === 3.14159 -> true ('a'..'j') === 'c' -> true ('a'..'j') === 'z' -> false |
Assignment:
Given a string s = 'key=value', create two strings s1 and s2 such that s1 contains key and s2 contains value. Hint: Use some of the String functions - program p021rangesex.rb