Ruby - Range operator (...)
A range represents an interval, a set of values with a beginning and an end. Values of a range can be numbers, characters, strings or objects. Using triple dot (...) operator is one of way which is used to construct ranges in Ruby. It provides flexibility to the code and reduce the size of code.
Ruby supports ranges to be used in a variety of ways.
- Ranges as Sequences
- Ranges as Conditions
- Ranges as Intervals
Ranges as Sequences
The most natural way to use ranges in Ruby is to produce successive values in the sequence. Sequence has a start point, an end point, and a way to produce successive values in the sequence. Triple Dot (...) operator creates a range from start point to end point exclusive.
(-1...-5) #=> [] (-5...-1) #=> [-5, -4, -3, -2] (1...5) #=> [1, 2, 3, 4] ('a'...'e') #=> ["a", "b", "c", "d"]
When used as an iterator, ranges return each value in the sequence. Consider the example below where to_a method is used to convert ranges into array.
range1 = (1...5).to_a range2 = ("abc"..."abg").to_a #displaying the result puts "#{range1}" puts "#{range2}"
The output of the above code will be:
[1, 2, 3, 4] ["abc", "abd", "abe", "abf"]
Ranges implement methods that let a user to iterate over them and test their contents in a number of ways. Consider the example below.
MyRange = 1...8 #checking the content retval = MyRange.include?(5) puts "MyRange contains 5?: #{retval}" #getting the minimum value retval = MyRange.min puts "MyRange minimum value: #{retval}" #getting the maximum value retval = MyRange.max puts "MyRange maximum value: #{retval}" #slicing the ranges retval = MyRange.select {|i| i < 5 } puts "MyRange selected: #{retval}" retval = MyRange.reject {|i| i < 5 } puts "MyRange rejected: #{retval}" #iterating over the range puts "MyRange contains:" MyRange.each do |i| puts i end
The output of the above code will be:
MyRange contains 5?: true MyRange minimum value: 1 MyRange maximum value: 7 MyRange selected: [1, 2, 3, 4] MyRange rejected: [5, 6, 7] MyRange contains: 1 2 3 4 5 6 7
Ranges as Conditions
Ranges can also be used to define conditional expressions. Consider the example below where conditional expressions are defined using ranges in a case statement.
marks = 55 grade = case marks when 0...36 then "Fail" when 36...51 then "Grade D" when 51...66 then "Grade C" when 66...81 then "Grade B" when 81...101 then "Grade A" else "Invalid Score" end puts grade
The output of the above code will be:
Grade C
Ranges as Intervals
Ranges can also be used to defined intervals to check that the given value falls within the interval or not. It is represented by equality operator(===).
i = 55 if((1...51) === i) puts "#{i} lies between 1 and 50." elsif((51...101) === i) puts "#{i} lies between 51 and 100." else puts "#{i} do not lie between 1 and 100." end
The output of the above code will be:
55 lies between 51 and 100.
❮ Ruby - Operators