RubyGuides
Share this post!

Ruby Ranges: How Do They Work?

Have you ever wondered how ranges work in Ruby?

Even if you haven’t, isn’t it fun to discover how things work under the hood?

That’s exactly what I’m going to show you in this post!

Understanding Ranges

Just as a reminder, this is what a Ruby range looks like:

The parenthesis are not necessary to define a Range, but if you want to call methods on your range you will need them (otherwise you are calling the method on the 2nd element of the range, instead of the range itself).

The Range class includes Enumerable, so you get all the powerful iteration methods without having to convert the range into an array.

Range has some useful methods, like the step method.

Example:

Other Range methods to be aware of are: cover? & include?. It would be a mistake to think that they do the same thing, because they don’t.

The include? method just does what you would expect, check for inclusion inside the range. So it would be equivalent to expanding the Range into an Array and checking if something is in there.

But cover? is different, all it does is check against the initial & ending values of the range (begin <= obj <= end), which can yield unexpected results.

Example:

The cover? example is equivalent to:

The reason this returns true is that strings are compared character by character. Since “a” comes before “c”, the characters that come after the first “c” don’t matter.

Range Implementation

Ranges are not limited to numbers & letters, you can use any objects as long as they implement the following methods: <=> and succ.

For example, here is a time range:

So how does this work? Let’s take a look at this implementation:

I added some comments to help you understand what is going on. The idea is that we keep calling the next method on the first object until it is equal to the second one, the assumption is that they will eventually meet.

Custom Class Ranges

Most of the time you will be using number & character ranges, but it’s still good to know how you can use ranges in a custom class.

Example:

The key here is to make sure that you implement the <=> & succ methods correctly.

If you want to use the include? method you need to include the Comparable module, which adds methods like ==, <, and > (all based on the results of the <=> method).

Conclusion

In this article you have learned how ranges work in Ruby so you can understand them better & implement your own objects that support range operations.

Don’t forget to subscribe to the newsletter below so you don’t miss the next post πŸ™‚

5 comments
rivsc says last year

Why ?

    Jesus Castello says last year

    Good question! The answer is probably this bit from the documentation.

    “If begin and end are numeric, comparison is done according to the magnitude of the values.”

    http://ruby-doc.org/core-2.2.0/Range.html#method-i-include-3F

    Hua says last year

    From the source:

    We can understand why.

Demba Siby says last year

‘The reason this returns false is that strings are compared character by character. Since β€œa” comes before β€œc”, the characters that come after the β€œc” don’t matter.’

You mean the reason this returns true?

    Jesus Castello says last year

    I fixed that. Thank you πŸ™‚

Comments are closed