Open In App

Ruby | Ranges

Improve
Improve
Like Article
Like
Save
Share
Report
Prerequisite: Ruby Range Operator

Ruby ranges depict a set of values with a beginning and an end. Values of a range can be numbers, characters, strings or objects. It is constructed using start_point..end_point, start_point…endpoint literals, or with ::new. It provides the flexibility to the code and reduce the size of code.

Example:




# Ruby program to demonstrate 
# the Range in Ruby
   
# Array value separator
$, =", " 
   
# using start_point..end_point
# to_a is used to convert it 
# into array
range_op = (7 .. 10).to_a
   
# displaying result
puts "#{range_op}"
   
# using start_point...end_point
# to_a is used to convert it 
# into array
range_op1 = (7 ... 10).to_a
   
# displaying result
puts "#{range_op1}"


Output:

[7, 8, 9, 10]
[7, 8, 9]


Ruby provides the 3 types of ranges as follows:

  1. Ranges as Sequences
  2. Ranges as Conditions
  3. Ranges as Intervals

Ranges as Sequences

This is a general and easy way to define the ranges in Ruby to produce successive values in the sequence. It has a start point and an end point. Two operators are used for creating ranges, one is Double Dot (..) operator and the another one is Triple Dot (…) operator.

Example:




# Ruby program to illustrate the ranges as sequences
  
#!/usr/bin/ruby   
  
# input the value which lies between 
# the range 6 and 8    
ranges = 6..8   
  
# print true if the value is lies
# between the range otherwise 
# print false 
puts ranges.include?(3)
  
# print the maximum value which lies
# between the range 
ans = ranges.max   
puts "Maximum value = #{ans}"   
  
# print the minimum value which lies
# between the range 
ans = ranges.min   
puts "Minimum value = #{ans}"   
    
  
# Iterate 3 times from 6 to 8
# and print the value
ranges.each do |digit|   
   puts "In Loop #{digit}"   
end  


Output:

false
Maximum value = 8
Minimum value = 6
In Loop 6
In Loop 7
In Loop 8

Ranges as Conditions

Ranges can also be defined as conditional expressions in looping. Here conditions are enclosed within the start and end statements.

Example:




# Ruby program to illustrate the ranges as condition
  
#!/usr/bin/ruby
  
# given number
num = 4152  
    
result = case num   
   when 1000..2000 then "Lies Between 1000 and 2000"   
   when 2000..3000 then "Lies Between 2000 and 3000"
   when 4000..5000 then "Lies Between 4000 and 5000" 
   when 6000..7000 then "Lies Between 6000 and 7000" 
         
   else "Above 7000"   
end   
    
puts result   


Output:

Lies Between 4000 and 5000

Ranges as Intervals

Ranges can also be defined in terms of intervals to check that the given value falls within the interval or not. It is represented by equality operator(===).

Example:




# Ruby program to illustrate the ranges as intervals
  
#!/usr/bin/ruby
  
# using if statement 
if (('A'..'Z') === 'D')
  
   # display the value
   puts "D lies in the range of A to Z"
  
# end of if
end
  
# using if statement 
if ((1..100) === 77)
  
  # display the value
  puts "77 lies in the range of 1 to 100"
  
# end of if
end


Output:

D lies in the range of A to Z
77 lies in the range of 1 to 100


Note: In Ruby, if you are trying to use reverse range operator then nothing will be returned. Because in the range operators if the right side value is small than the left side value then they returned nothing. In order to print a reverse order of given range, always use the reverse() method with range operators.




# Ruby program to print the reverse
# order using the range operator
  
#!/usr/bin/ruby
  
# using ranges
# but it will not give any output
puts ('Z'..'W').to_a 
   
# using reverse() method which will 
# print given range in the reverse order
puts ('W'..'Z').to_a.reverse   


Output:

Z
Y
X
W


Last Updated : 14 Jun, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads