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:
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.
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:
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:
for i in 0..4
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.
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:
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!"
puts "I am completely lost!"
The above code uses ranges to check for a few range brackets and evaluate if the random value is within either set.
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.
puts "Maybe check other places"
puts "I could be here"
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).cover? 1
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.