0
0
Rubyprogramming~15 mins

Range operators (.. and ...) in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Range operators (.. and ...)
📖 Scenario: You are organizing a small library and want to select books by their ID numbers. You will use Ruby's range operators to pick books within certain ID ranges.
🎯 Goal: Learn how to create ranges using the .. and ... operators and understand the difference between inclusive and exclusive ranges.
📋 What You'll Learn
Create a range of book IDs using the inclusive range operator ..
Create a range of book IDs using the exclusive range operator ...
Use a for loop to iterate over the inclusive range
Print the ranges to see the difference in output
💡 Why This Matters
🌍 Real World
Ranges are useful when you want to select a sequence of items, like book IDs, page numbers, or dates, without listing each item individually.
💼 Career
Understanding ranges helps in data filtering, pagination, and working with sequences in many programming jobs.
Progress0 / 4 steps
1
Create an inclusive range of book IDs
Create a variable called inclusive_range that holds a range of book IDs from 1 to 5 using the inclusive range operator ...
Ruby
Need a hint?

Use .. to include the last number in the range.

2
Create an exclusive range of book IDs
Create a variable called exclusive_range that holds a range of book IDs from 1 to 5 using the exclusive range operator ....
Ruby
Need a hint?

Use ... to exclude the last number in the range.

3
Iterate over the inclusive range
Use a for loop with the variable book_id to iterate over inclusive_range and print each book_id.
Ruby
Need a hint?

Use for book_id in inclusive_range and puts book_id inside the loop.

4
Print both ranges to compare
Print inclusive_range.to_a and exclusive_range.to_a to see the difference between the two ranges.
Ruby
Need a hint?

Use puts inclusive_range.to_a and puts exclusive_range.to_a to print the ranges as arrays.