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
a = 12
b = 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
Ruby
a = Range . new ( 2 , 5 , false )
b = Range . new ( 2 , 5 , false )
a == b
|
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
Ruby
case 25 . 67
when 1 ... 55 then puts "Lower"
when 55 ... 85 then puts "Medium"
when 85 ... 100 then puts "Upper"
end
|
Lower
- begin : This method return first object of rng.
rng.begin --> obj
Ruby
b = Range . new ( 3 , 9 , false )
b. begin
|
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
Ruby
( 40 .. 45 ). each do |i|
print i, '....'
end
|
40....41....42....43....44....45....
- end : This method returns the end object of rng.
rng.end --> obj
Ruby
a = Range . new ( 3 , 9 , false )
a. end
|
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
Ruby
a = Range . new ( 2 , 5 , false )
b = Range . new ( 2 , 5 , false )
a.eql?(b)
|
true
- exclude_end? : This method return true if the end of the rng is omitted, otherwise return false.
rng.exclude_end? --> true or false
Ruby
a = Range . new ( 3 , 9 , false )
a.exclude_end?
|
false
- first : This method return starting object of rng.
rng.first --> obj
Ruby
a = Range . new ( 3 , 9 , false )
a.first
|
3
- last : This method return the last object of rng.
rng.last --> obj
Ruby
a = Range . new ( 3 , 9 , false )
a.last
|
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
true
- include? : This method returns true if obj is an element of rng, false otherwise.
rng.include?(value) --> true or false
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
Please Login to comment...