A range in Ruby is an object that represents a sequence of values from a start to an end.
By default, a range includes a start and an end value and can be either inclusive (..) or exclusive (...).
Example
# Create a range from 0 to 3 (inclusive)
numbers = 0..3
# Iterating through the range
numbers.each do |i|
puts i
end
Output
0 1 2 3
Range Syntax
start..end # includes end value
start...end # excludes end value
You can use integers, letters, or any object that can be compared and incremented to define the range.
Range Return Value
In Ruby, the .. and ... operators are used to create a Range object.
To access the values inside, you can convert this range into an array using the .to_a method.
Example 1: Inclusive Range
# Create a range from 0 to 3 (3 is included)
numbers = 0..3
# Convert to array and print it
puts numbers.to_a # Output: [0, 1, 2, 3]
In this example, the range 0..3 is converted into an array using .to_a, which returns all values including the end.
Example 2: Exclusive Range
# Create an exclusive range that starts at 0 and ends before 4
numbers = 0...4
puts numbers.to_a # Output: [0, 1, 2, 3]
# Creates an empty range
empty = 5...5
puts empty.to_a # Output: []
Here, 0...4 creates a range that excludes 4, so the resulting array goes from 0 to 3.
Example 3: Character Ranges
# Lowercase letters
letters = 'a'..'d'
puts letters.to_a # Output: ["a", "b", "c", "d"]
# Uppercase letters
caps = 'A'...'E'
puts caps.to_a # Output: ["A", "B", "C", "D"]
In this example, 'a'..'d' includes 'd', while 'A'...'E' excludes 'E'.
Range in Loop
Ranges are often used in loops to repeat an action a specific number of times.
Here's an example using a for loop:
# Iterate the loop five times
for i in 0...5
puts "#{i} Hello"
end
Output
0 Hello 1 Hello 2 Hello 3 Hello 4 Hello
Here, the range 0...5 runs the loop from 0 up to, but not including, 5.
Range in Conditions
Ranges can also be used to check if a value lies within a certain span. For example,
age = 25
if (18..30).include?(age)
puts "You are a young adult."
end
Output
You are a young adult.
In this example, the condition evaluates to true because 25 lies within the range 18..30.
Range in Case Statement
Ranges are commonly used in case statements to simplify multiple comparisons. For example,
score = 78
case score
when 90..100
puts "Grade A"
when 80...90
puts "Grade B"
when 70...80
puts "Grade C"
else
puts "Grade D or lower"
end
Output
Grade C
Here, 78 matches the range 70...80, so it prints Grade C.
Beginless/Endless Ranges
Ruby also supports ranges with no beginning or end. For example,
# Beginless range: includes all values less than or equal to 10
range = (..10)
puts range.include?(5) # true
puts range.include?(15) # false
Here, (..10) matches any value ≤ 10.
# Endless range: includes all values greater than or equal to 5
range = (5..)
puts range.include?(100) # true
puts range.include?(1) # false
Here, (5..) matches any value ≥ 5.
Common Ruby Range Methods
| Method | Description |
|---|---|
.to_a |
Converts the range into an array. |
.include?(value) |
Checks if the given value exists within the range. |
.first |
Returns the first value in the range. |
.last |
Returns the last value in the range. |
.size |
Returns the number of elements in the range (works only with numeric ranges). |
.step(n) |
Iterates over the range in steps of n; can be used with a block or .to_a. |
More on Ruby Range
To skip numbers in the sequence, you can use .step. For example,
# From 1 to 10, step by 2
(1..10).step(2) do |i|
puts i
end
Output
1 3 5 7 9
Here, .step(2) increments the value by 2 on each iteration within the range.