A range refers to an object that registers a specific sequence. We define ranges with a particular start and ending value, then spread out the values between the range of start and end values.
A simple example could be of values from 10 to 100. Between the value of 10 and 100, you can have the sequence to include only the even numbers and such.
Using this guide, we will learn how to create ranges in Ruby, work with the step method, and finally, convert a range to an array.
Getting Started with Ranges
We use the double dot (..) and triple dot (…) to create a range in Ruby.
The double dot notation produces a range of values, including the start and end values of the range.
On the other hand, the three-dot notation will exclude the end (high) value from the list of values.
Consider the example below:
Both of the examples above are valid Ruby ranges. However, the output of the values is as below (respectively).
1, 2, 3, 4, 5, 6, 7, 8, 9
To test whether the range includes a value, we can use various methods such as include? Take a look at the example below:
puts (1...10).include?(10) # => false
The values from a range are not denoted as a list. They are actual Range objects of the Range class that contains Enumerable. Hence, we can execute iteration operations on a Range without converting it to an array object.
Consider a simple example below:
Using a simple for loop, we can fetch each item in the range and act on the value.
Another method we can apply to a range is the reject method. This method will remove all the values that do not meet a specific condition. F
For example, take a range from 1 to 100; we can reject all the values that are not numerically even.
Below is an example code:
Ruby is so flexible that it allows you to do more with a few lines of code.
In our example above, we use built-in Ruby methods to filter out the range and only include the even values .
Below is an example output:
The Step Method in Ruby
Suppose we want to create a range but over a specific interval? In such a scenario, we can use the step method. This method will increment the values in the range (between start and end) over an increment of the specified value.
Let us illustrate this with an example:
NOTE: Ignore the to_a method (discussed later).
The above example should give an output similar to the one shown below:
As you will notice, we add 3 to the current value or print every third item from the current range position.
Range to Array
Can you remember the to_a method mentioned above? Let us discuss what it means and how it works. The to_a method helps convert a sequence of values to an array.
For example, consider the example below:
The above example will return the values from the range in an array format. Think of it as looping over each item in the range and appending it to an array. Here is a simple illustration.
for i in (1...10)
arr = arr.push(i)
We start by defining an empty array. Next, we iterate over items in a range and push each item to the collection.
Ruby also allows you to use the Array() method to create a new array from the items in a range.
Consider the example below:
Yes, the method does look like an array class. However, you need to add a pair of parenthesis to let Ruby know you are using the Array method and not the class.
The resulting value is the range of values in an array format.
This guide provides you with information on how to work with Ruby ranges. Using this guide, you now understand how to work with ranges and convert them to an array.
Thank you for reading, and stay tuned for more Ruby guides.