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 program to illustrate # new Method a = 12b = 15 # Output will be 12..15 Range.new(a, b, false) |
Output:
12..15
Instance Methods
- == : This method return true if obj whose staring, 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 program to illustrate# the use of == method# Using Range.new class methoda =Range.new(2,5,false)b =Range.new(2,5,false)# Using == instance methoda == bchevron_rightfilter_noneOutput:
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 program to illustrate the use# of === method by case statement# taking case statementcase25.67when1...55thenputs"Lower"when55...85thenputs"Medium"when85...100thenputs"Upper"endchevron_rightfilter_noneOutput:
Lower
- begin : This method return first object of rng.
rng.begin --> obj
Example:
# Ruby program to illustrate# the use of begin method# Creating range using the new methodb =Range.new(3,9,false)# using begin instance methodb.beginchevron_rightfilter_noneOutput:
3
- each : This method is used to iterate over elements of rng, by passing each in turn to the block.
rng.each{|j| block} --> rngExample:
# Ruby program to illustrate# the use of each method# using each method(40..45).eachdo|i|print i,'....'endchevron_rightfilter_noneOutput:
40....41....42....43....44....45....
- end : This method returns the end object of rng.
rng.end --> obj
Example:
# Ruby program to illustrate# the use of end method# using end methoda =Range.new(3,9,false)a.endchevron_rightfilter_noneOutput:
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 flage as rng then it returns true, otherwise it returns false.
rng.eql?(obj) --> true or false
Example:
# Ruby program to illustrate# the use of eql? method# Constructing rangesa =Range.new(2,5,false)b =Range.new(2,5,false)# using eql? methoda.eql?(b)chevron_rightfilter_noneOutput:
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 program to illustrate the# use of exclude_end? method# constructing rangea =Range.new(3,9,false)# using exclude_end? methoda.exclude_end?chevron_rightfilter_noneOutput:
false
- first : This method return starting object of rng.
rng.first --> obj
Example:
# Ruby program to illustrate# the use of first method# constructing rangea =Range.new(3,9,false)# using the first methoda.firstchevron_rightfilter_noneOutput:
3
- last : This method return the last object of rng.
rng.last --> obj
Example:
# Ruby program to illustrate# the use of last method# constructing rangea =Range.new(3,9,false)# using last methoda.lastchevron_rightfilter_noneOutput:
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 program to illustrate# the use of member? method# taking a rangea =1..10# using member? methoda.member?(5)chevron_rightfilter_noneOutput:
true
- include? : This method returns true if obj is an element of rng, false otherwise.
rng.include?(value) --> true or false
Example:
# Ruby program to illustrate# the use of include? method# using include? method("A".."G").include?("Z")chevron_rightfilter_noneOutput:
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
Recommended Posts:
- Ruby | Thread Class-Public Class Methods
- Ruby | Exception Class and its Methods
- Ruby | Dir Class and its methods
- Ruby | Float Class
- Ruby | Integer Class
- Ruby | Symbol Class
- Ruby | Struct Class
- Ruby | Hash Class
- Ruby | MatchData Class
- Ruby | Exception Class and its Methods
- Ruby | MatchData Class
- Ruby | Dir Class and its methods
- Ruby | Struct Class
- Ruby | Hash Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

