Home » Ruby programming

Ruby | Ranges with Examples

Ruby ranges: Here, we are going to learn about the ranges in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on September 28, 2019

Ruby Ranges

Ranges are the operators which are symbolized as ".." and "..." and have a start point and an endpoint. You can call them double dot and triple dot operators respectively. The difference between both the operators is that double dot operator is the inclusive range operator and triple dot is an exclusive range operator which means that if you print (1..4), it will print numbers from 1 to 4 whereas if you print (1…4), it will print numbers from 1 to 3. Ranges are used to decrease the code size and to increase the flexibility of the Ruby code.

Types of Ranges

You get three types of Ranges in Ruby namely:

  1. Ranges as sequences
  2. Ranges as intervals
  3. Ranges as conditions

Now, let us discuss them with examples for a better understanding of the concept.

1) Ranges as sequences

When you use Ranges as a sequence, you are trying to get successive values in a particular sequence. You are supposed to specify a starting point in the form of an integer and an endpoint. You can use the double dot operator as well as the triple-dot operator as per the requirement of your code.

Let us understand its implementation with the help of example given below:

=begin
Ruby program to demonstrate 
ranges as sequences using .. operator.
=end

myrange = 4..8

maxv = myrange.max 
puts "Maximum value from range = #{maxv}"

minv = myrange.min 
puts "Minimum value from range = #{minv}"
	
if myrange.include?(8) then
	puts "8 is present"
end

myrange.each do |digit| 
    puts "#{digit} * #{digit} = #{digit*digit}"
end

Output

Maximum value from range = 8
Minimum value from range = 4
8 is present
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49
8 * 8 = 64

In the above code, first we are printing maximum and minimum value out of range and then we are checking the presence of a particular element. In the end, we are printing all the elements of the range.

Now, let us look at the same example with the help of triple dot operator "...".

=begin
Ruby program to demonstrate 
ranges as sequences using ... operator.
=end

myrange = 4...8

maxv = myrange.max 
puts "Maximum value from range = #{maxv}"

minv = myrange.min 
puts "Minimum value from range = #{minv}"
	
if myrange.include?(8) then
	puts "8 is present"
else
	puts "8 is not present"
end
myrange.each do |digit| 
    puts "#{digit} * #{digit} = #{digit*digit}"
end

Output

Maximum value from range = 7
Minimum value from range = 4
8 is not present
4 * 4 = 16
5 * 5 = 25
6 * 6 = 36
7 * 7 = 49

Now, you can see that when we use the triple-dot operator, it is not including the endpoint in the range.

2) Ranges as Condition

You can also use range as a conditional statement inside a loop. We often use it with for loop. Refer the example given below:

=begin
Ruby program to demonstrate 
ranges as conditions using ... operator.
=end

puts "Enter start point:-"
start = gets.chomp.to_i

puts "Enter end point:-"
endpoint = gets.chomp.to_i

puts "--------------"
for i in start..endpoint
    puts i*10
end

Output

Enter start point:-
10
Enter end point:-
20
--------------
100
110
120
130
140
150
160
170
180
190
200

You can also implement range as a condition in the switch case statement. See the example given below:

=begin
Ruby program to demonstrate 
ranges as conditions using .. operator.
=end

puts "Enter the number"
num = gets.chomp.to_i	

rslt = case num
when 1000..2000 then "Between 1000 and 2000"
when 2000..3000 then "Between 2000 and 3000"
when 4000..5000 then "Between 4000 and 5000"
when 6000..7000 then "Between 6000 and 7000"
else "Above 7000"
end

puts rslt

Output

Enter the number
1200
Between 1000 and 2000

You can observe that when the user enters 1200, it prints 'Between 1000 and 2000' because 1200 lies between 1000 and 2000.

3) Ranges as Intervals

You can also use ranges to check the presence of a number in the range. We use "===" operator in this case. See the following code:

=begin
Ruby program to demonstrate 
ranges as intervals using .. operator.
=end

puts "Enter the Alphabet"
alpha = gets.chomp

if (('A'..'T') === alpha) 
    puts "#{alpha} lies in the range of A to T"
else
    puts "#{alpha} does not lies in the range of A to T"
end

Output

RUN 1:
Enter the Alphabet
V
V does not lies in the range of A to T

RUN 2:
Enter the Alphabet
F
F lies in the range of A to T

You can observe in the above code that we are using === operator to implement range as an interval.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.