Ranges are everywhere in our daily life. For example, it could be how long it will take to read this article, the distance between Moscow and London, the number of days between now and Christmas, etc.

Although ranges may be implemented differently in a programming language, the core concept does not differ.

In this tutorial, we will learn how to use ranges in the Ruby programming language.

What is a Range?

What is a range, specifically? A range is a set of values with a starting point and ending point.

In programming, a range can be a set of numbers, characters, strings, and other objects. The general idea is that a range should have a start and end.

How to Create a Range in Ruby

The simplest way to create a range in Ruby is to use dot notations. The following example illustrates how a range works in Ruby:

for i in 1..10


    puts i

end

The example above should return all the items from a range of 1 to and including 10.

Ruby uses dot notations to indicate the range of values.

Two dots (..) is called an inclusive range because it includes the end value in the items. For example, 1..10 will start from 1 up to and including 10.

Three dots (…) is called the exclusive range because it does not include the end value in the range. For example, 1…10 starts from 1 and ends at 9.

Sequences

The basic implementation of Ruby ranges is to generate or go through a sequence of values. In the previous example, we used a range inside a for loop to generate a sequence of values between 1 and 10 (inclusive).

We can also use range to loop the items in an array, especially when we know the total items.

Consider the array below:

databases = [“MySQL”, “PostgreSQL”, “Redis”, “MongoDB”, “Firestore”]

To get the items in the array, we can use the item indexes, which would lead to 5 repetitive pieces of code.

We can use a range as:

databases = [“MySQL”, “PostgreSQL”, “Redis”, “MongoDB”, “Firestore”]

for i in 0..4


    puts databases[i]

end

Although the above implementation is not the most efficient way to do such an operation, it is easier to read than a bunch of repetitive indexes.

It is good to note that ranges are not limited to integer values only. We can generate all the letters in the alphabet using a simple range.

The above code will generate all the characters between a and z and put them into an array using the to_a method.

Conditionals

Ranges are also implementable in conditional expressions. For example, suppose we want to determine if a value is between various groups of numbers.

Let us take a random number and ask: if the number between 1 and 10, 20 and 30, 40 and 50, and such.

We can implement this using a few lines of code as:

random = 34


correct = case random

when 1..10 then “I am here”

when 11..20 then “I am above 10”

when 21..30 then “Try above 20”

when 31..40 then “I am above 15 x 2”

when 41..50 then “I am halfway!”

else


    puts “I am completely lost!”

end

puts correct

The above code uses ranges to check for a few range brackets and evaluate if the random value is within either set.

Intervals

The final implementation of Ruby ranges is testing if a value lies within an interval of other values. It is hardly distinguishable from conditionals, but it uses logical operators to evaluate.

For example:

if ((1..10)) === 7


    puts “Maybe check other places”

elsif


    puts “I could be here”

end

The simple implementation above checks whether the value 7 is within the range of 1 and 10 and executes a code if true.

Include and Cover

There are two methods you can use when working with Ruby ranges. These are the include and the cover.

The include method works by checking if a value is in the range, while the cover checks the initial and ending value of the range for a match.

puts (1..10).include? 10

puts (1..10).cover? 1

Closing

In this quick guide, we discussed the basics of using Ranges in Ruby. Although there is more, the concept discussed in this tutorial should help you understand ranges in Ruby. To learn more, check out the official documentation.

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/08/echo/john-150×150.png61213d46d9e84.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list