Open In App

Ruby | Range Class Methods

Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite: Ruby | Ranges

Ruby provides a Range class. 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. You can also create a range by using Range.new. A range which contains ..(two dots) means running from the starting value to the end value inclusively and if a range contains (three dots) means it exclude the end value. 
Example:
 

(1..6).to_a    # Output= [1, 2, 3, 4, 5, 6]
(1...6).to_a    # Output= [1, 2, 3, 4, 5]

Note: In Ruby, Ranges can be created using the objects, as long as the objects can be compared using their <=> operator. To return the next object in sequence, it provides the support to the succ method. 
 

Class Method

new : This method is used to create a range from given start and end value. In this method, if the third parameter is false or excluded, the range includes end-object. Otherwise, it will omit.
 

Range.new(start, end, exclusive=false)

Example:

Ruby




# Ruby program to illustrate
# new Method
 
a = 12
b = 15
 
# Output will be 12..15
Range.new(a, b, false


Output: 

12..15

 

Instance Methods

  • == : This method return true if obj whose starting, ending and value of third parameter is same as rng. Otherwise, it will return false. The return type of this method is boolean.
rng==obj-->true or false
  • Example: 

Ruby




# Ruby program to illustrate
# the use of == method
 
# Using Range.new class method
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
 
# Using == instance method
a == b                  


  • Output:  
true
  • === : In this method if rng omits its end, then return rng.start <= value < rng.end and if rng is inclusive, then it return rng.start <= value<= rng.end. Conveniently, === is the comparison operator and case statements uses this.
rng===value --> true or false
  • Example: 

Ruby




# Ruby program to illustrate the use
# of === method by case statement
 
# taking case statement
case 25.67
 
when 1...55 then puts "Lower"
when 55...85 then puts "Medium"
when 85...100 then puts "Upper"
end                                     


  • Output: 
Lower
  • begin : This method return first object of rng.
rng.begin --> obj
  • Example: 

Ruby




# Ruby program to illustrate
# the use of begin method
 
# Creating range using the new method
b = Range.new(3, 9, false)
 
# using begin instance method
b.begin               


  • Output:  
3
  • each : This method is used to iterate over elements of rng, by passing each in turn to the block.  
rng.each{|j| block} --> rng
  • Example:

Ruby




# Ruby program to illustrate
# the use of each method
  
# using each method
(40..45).each do |i|
print i, '....'
end   


  • Output:
40....41....42....43....44....45....
  • end : This method returns the end object of rng
rng.end --> obj
  • Example: 

Ruby




# Ruby program to illustrate
# the use of end method
 
# using end method
a = Range.new(3, 9, false)
a.end                


  • Output: 
9
  • eql? : This method check if obj and rng are equal in terms of start, end and exclusive flag. If obj contains the same value of start, end and exclusive flag as rng then it returns true, otherwise it returns false. 
rng.eql?(obj) --> true or false
  • Example:

Ruby




# Ruby program to illustrate
# the use of eql? method
  
# Constructing ranges
a = Range.new(2, 5, false)
b = Range.new(2, 5, false)
 
# using eql? method
a.eql?(b)              


  • Output: 
true
  • exclude_end? : This method return true if the end of the rng is omitted, otherwise return false.  
rng.exclude_end? --> true or false
  • Example:

Ruby




# Ruby program to illustrate the
# use of exclude_end? method
  
# constructing range
a = Range.new(3, 9, false)
 
# using exclude_end? method
a.exclude_end?    


  • Output: 
false
  • first : This method return starting object of rng.
rng.first --> obj
  • Example:

Ruby




# Ruby program to illustrate
# the use of first method
  
# constructing range
a = Range.new(3, 9, false)
 
# using the first method
a.first             


  • Output:  
3
  • last : This method return the last object of rng
rng.last --> obj
  • Example:

Ruby




# Ruby program to illustrate
# the use of last method
  
# constructing range
a = Range.new(3, 9, false)
 
# using last method
a.last             


  • Output:  
9
  • member? : This method check if the given value is the member of rng. If the given value is the member of rng then it return true, otherwise return false. 
rng.member?(value) --> true or false
  • Example:

Ruby




# Ruby program to illustrate
# the use of member? method
  
# taking a range
a = 1..10
 
# using member? method
a.member?(5)          


  • Output:  
true
  • include? : This method returns true if obj is an element of rng, false otherwise.  
rng.include?(value) --> true or false
  • Example:
     

Ruby




# Ruby program to illustrate
# the use of include? method
  
# using include? method
("A".."G").include?("Z")


  • Output:  
false
  • step : This method iterates over rng, by passing each nth object to the block. If the range have numbers, addition to one generate successive elements. Otherwise step invokes succ method to iterate range elements.
     
rng.step(n=1){|obj|block} --> rng

Reference: https://ruby-doc.org/core-1.9.3/Range.html
 



Last Updated : 22 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads