Ruby Range

A range is an object that represents a sequence of values from a start to an end. By default, a range can be either inclusive (..) or exclusive (...) of its end value.

Here's a simple example of range. Read the rest of the tutorial to learn more.

Example

# Create a range from 0 to 3 (inclusive)
numbers = 0..3

# Iterating through the range
numbers.each do |i|
  print "#{i}, " 
end

# Output: 0, 1, 2, 3, 

Here, the inclusive range 0..3 contains numbers from 0 to 3, including the end value 3.


Range Syntax

# Includes the end value
start_value..end_value

# Excludes the end value
start_value...end_value

You can use integers, letters, or any object that can be compared and incremented to define the range.


Return Value of a Range

In Ruby, the .. and ... operators are used to create a Range object. In other words, ranges return an object of Range type. For example,

numbers = 0..3

puts numbers.class

# Output: Range

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
p 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

p numbers.to_a

# Output: [0, 1, 2, 3]

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'
p letters.to_a

# Uppercase letters
caps = 'A'...'E'
p caps.to_a

Output

["a", "b", "c", "d"]
["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 that's less than or equal to 10 (≤ 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 that's greater than or equal to 5 (≥ 5).


Common Ruby Range Methods

Some common Range methods are given below:

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.

Example 4: Range Methods

Let's look at a few Range methods we haven't used before:

numbers = 1..10

# Get the first range element
puts "First Element: #{numbers.first}"

# Get the last range element
puts "Last Element: #{numbers.last}"

# Get the size of the range
puts "Range Size: #{numbers.size}"

puts "\nStepping by 2"

# From 1 to 10, step by 2
numbers.step(2) do |i|
  puts i
end

Output

First Element: 1
Last Element: 10
Range Size: 10

Stepping by 2
1
3
5
7
9
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges