Ruby | Range Class Methods (original) (raw)

Last Updated : 22 Aug, 2022

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

rng==obj-->true or false

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

`

true

rng===value --> true or false

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

`

Lower

rng.begin --> obj

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

`

3

rng.each{|j| block} --> rng

Ruby program to illustrate

the use of each method

using each method

(40..45).each do |i| print i, '....' end

`

40....41....42....43....44....45....

rng.end --> obj

Ruby program to illustrate

the use of end method

using end method

a = Range.new(3, 9, false) a.end

`

9

rng.eql?(obj) --> true or false

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)

`

true

rng.exclude_end? --> true or false

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?

`

false

rng.first --> obj

Ruby program to illustrate

the use of first method

constructing range

a = Range.new(3, 9, false)

using the first method

a.first

`

3

rng.last --> obj

Ruby program to illustrate

the use of last method

constructing range

a = Range.new(3, 9, false)

using last method

a.last

`

9

rng.member?(value) --> true or false

Ruby program to illustrate

the use of member? method

taking a range

a = 1..10

using member? method

a.member?(5)

`

true

rng.include?(value) --> true or false

Ruby program to illustrate

the use of include? method

using include? method

("A".."G").include?("Z")

`

false

rng.step(n=1){|obj|block} --> rng

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