Ruby Logo

Ruby Ranges Mastery

Master Ruby ranges with inclusive vs exclusive examples, array slicing, and range operations.

Home Ruby Ruby Ranges Mastery

Ruby Ranges Mastery

Ranges are powerful Ruby objects that represent a sequence of values. They're essential for iteration, slicing, and creating elegant, readable code. Master inclusive vs exclusive ranges and their many applications.

Range Basics & Types

Ranges: Ruby ranges represent sequences of values with a start and end point. They can be inclusive (..) or exclusive (...) and work with numbers, strings, dates, and custom objects.

Range Types

# Inclusive range (includes end value)
inclusive_range = 1..10
puts
inclusive_range.to_a
# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Exclusive range (excludes end value)
exclusive_range = 1...10
puts
exclusive_range.to_a
# [1, 2, 3, 4, 5, 6, 7, 8, 9]

# String ranges
letter_range = 'a'..'z'
puts
letter_range.to_a.first(5)
# ["a", "b", "c", "d", "e"]

# Date ranges
require 'date'
date_range = Date.new(2024, 1, 1)..Date.new(2024, 1, 7)
puts
date_range.to_a.map(&:strftime).first(3)
# ["2024-01-01", "2024-01-02", "2024-01-03"]
Inclusive Range (..)

Includes the end value in the sequence

Exclusive Range (...)

Excludes the end value from the sequence

Range Methods & Operations

Essential Range Methods

# Range properties
range = 1..10
puts
range.begin
# 1
puts
range.end
# 10
puts
range.exclude_end?
# false

# Membership testing
puts
range.include?(5)
# true
puts
range.cover?(5)
# true
puts
range.include?(15)
# false

# Range size and count
puts
range.size
# 10
puts
range.count
# 10
puts
range.count { |x| x.even? }
# 5

Range Iteration

# Basic iteration
(1..5).each
do
|i|
puts "Number: #{i}"
end

# Step iteration
(1..10).step(2)
do
|i|
puts "Even step: #{i}"
end

# Reverse iteration
(1..5).reverse_each
do
|i|
puts "Reverse: #{i}"
end

Array Slicing with Ranges

Range-based Array Slicing

# Array slicing with ranges
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

puts
numbers[1..5]
# [1, 2, 3, 4, 5] (inclusive)
puts
numbers[1...5]
# [1, 2, 3, 4] (exclusive)
puts
numbers[..3]
# [0, 1, 2, 3] (from start)
puts
numbers[7..]
# [7, 8, 9] (to end)

# String slicing
text = "Hello, World!"
puts
text[0..4]
# "Hello"
puts
text[7..-2]
# "World"

Interactive Practice: Ranges

Practice Time: Try these range examples and experiment with different range operations. Understanding ranges will make you a more effective Ruby programmer.

Interactive Code Runner

Ruby Code Editor
Output will appear here when you run the code...

Range Best Practices

✅ Do This

  • Use inclusive ranges for complete sequences
  • Use exclusive ranges for array indexing
  • Leverage ranges for array slicing
  • Use step for non-contiguous sequences
  • Consider memory usage with large ranges

❌ Avoid This

  • Creating ranges with non-comparable objects
  • Using ranges for very large sequences
  • Mixing inclusive/exclusive without purpose
  • Forgetting that ranges are lazy by default
  • Using ranges where arrays are more appropriate

Ruby Ranges Mastery Checklist

  • Range Types: Understand inclusive (..) vs exclusive (...) ranges
  • Range Methods: Master begin, end, include?, cover?, size, count
  • Iteration: Use each, step, reverse_each for range iteration
  • Array Slicing: Apply ranges to arrays and strings for slicing
  • Membership: Test values with include? and cover?
  • Performance: Understand lazy evaluation and memory usage
  • Data Types: Use ranges with numbers, strings, dates, and custom objects

Next Steps: Practice with ranges in array slicing, iteration patterns, and data validation. Ranges are fundamental to Ruby's elegant syntax and powerful iteration capabilities!

Quick Navigation

Related Topics

Video Tutorial

Watch and learn ruby ranges mastery

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!