Ranges
Ruby is a dynamic, beginner-friendly programming language, and a Range is a compact way to express a sequence between two endpoints — perfect for loops, slicing, and bucketing values.
Learn Ranges in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll build inclusive and exclusive ranges, iterate them, and use them in case statements and slices.
What You'll Learn in This Lesson
1️⃣ Building and Iterating Ranges
Two dots 1..5 include the end; three dots 1...5 exclude it. Ranges respond to all the Enumerable methods you've seen — each , sum , include? — and you can turn one into an array with to_a .
2️⃣ Ranges in case, step, and Slicing
Ranges make case/when read beautifully for buckets like grades or age brackets. cover? checks membership instantly without building a list, step walks in increments, and a range can slice arrays and strings.
Your turn. Print a times table and a sum using ranges. The TODO s remind you to keep the range inclusive — run it for a few values of n .
Filter a range down to its even numbers, then sum them. This previews select from the next lesson. Run with ruby evens.rb .
📋 Quick Reference — Ranges
Practice quiz
What does (1..5).to_a return?
Two dots .. include the end, so (1..5) covers 1 through 5.
What does (1...5).to_a return?
Three dots ... exclude the end, so (1...5) covers 1 through 4.
What does (1..100).sum return?
- 100
- 5000
- 5050
- 10100
Answer: 5050. Ranges respond to Enumerable methods; (1..100).sum is 5050.
What does (1..1_000_000).cover?(500_000) return?
- true
- false
- nil
- an error
Answer: true. cover? checks membership instantly from the endpoints without building a list.
What does (0..10).step(2).to_a produce?
step(2) walks the inclusive range in increments of 2: 0, 2, 4, 6, 8, 10.
What does (5..1).to_a return?
A range with start greater than end is empty, not a countdown.
What does "Hello"[0..2] return?
- "He"
- "Hel"
- "Hello"
- "ello"
Answer: "Hel". A range slices the string by index 0 through 2 inclusive, giving "Hel".
Why is cover? often preferred over include? for very large ranges?
- cover? is O(1) on endpoints; include? may scan
- include? doesn't exist
- cover? builds an array
- They are identical
Answer: cover? is O(1) on endpoints; include? may scan. cover? checks endpoints in O(1) while include? may iterate.
Which kind of object can form a range besides numbers?
- Only floats
- Letters/strings like ("a".."e")
- Only symbols
- Hashes
Answer: Letters/strings like ("a".."e"). Any comparable object with a successor works, e.g. letter ranges ("a".."e").
In a case/when, which range matches a score of 73 among 90..100, 80...90, 70...80?
- 90..100
- 80...90
- 70...80
- none
Answer: 70...80. 73 falls in 70...80 (exclusive of 80), which Ruby matches with ===.