TExcelTAD
Home › Ruby › Ruby Ranges

Ruby Ranges

A range represents a sequence of values from a start to an end, such as 1..10 or "a".."z". Ranges are compact (they don't store every element), and they are used for loops, slicing, conditions and case statements.

Inclusive and exclusive

ruby · try it yourself
p (1..5).to_a      # two dots: end included
p (1...5).to_a     # three dots: end excluded
p (1..5).size
p (1...5).size
p ("a".."e").to_a
[1, 2, 3, 4, 5]
[1, 2, 3, 4]
5
4
["a", "b", "c", "d", "e"]

A way to remember it: the extra dot in ... "pushes" the last value out.

Range Contains Excludes
1..10 1 to 10 —
1...10 1 to 9 10
0.0...1.0 every float from 0 up to (not including) 1 1.0

Ranges in loops

ruby · try it yourself
(1..4).each { |n| print n * n, " " }
puts
(10..20).step(5) { |n| print n, " " }
puts
(1..3).reverse_each { |n| print n, " " }
puts
p (1..10).select(&:even?)
p (1..100).sum
1 4 9 16
10 15 20
3 2 1
[2, 4, 6, 8, 10]
5050

Ranges include the Enumerable module, so most array methods (map, select, sum, each_slice, …) work on them directly.

Checking membership

ruby · try it yourself
adult = 18..64
p adult.include?(30)
p adult.cover?(70)
p adult === 18
p 25.between?(18, 64)
p (1.0..2.0).include?(1.5)
p ("a".."z").cover?("m")
true
false
true
true
true
true

include? and cover? are usually the same for numbers. cover? only compares against the ends, which makes it fast even for huge ranges.

Ranges in case/when

ruby · try it yourself
def ticket_price(age)
  case age
  when 0..2 then 0
  when 3..12 then 8
  when 13..64 then 14
  else 9
  end
end

[1, 9, 30, 70].each { |a| puts "age #{a}: #{ticket_price(a)} EUR" }
age 1: 0 EUR
age 9: 8 EUR
age 30: 14 EUR
age 70: 9 EUR

Endless and beginless ranges

Leave out the end (5..) or the start (..5) for an open-ended range:

ruby · try it yourself
letters = %w[a b c d e f]
p letters[2..]
p letters[..1]
p letters[-2..]

grade = ->(score) do
  case score
  when 90.. then "A"
  when 70...90 then "B"
  when ...70 then "C"
  end
end
p [95, 72, 40].map(&grade)

(1..).each do |n|
  break if n > 3
  print n
end
puts
["c", "d", "e", "f"]
["a", "b"]
["e", "f"]
["A", "B", "C"]
123

Endless ranges are perfect for "from here to the end" slices of arrays and strings.

Slicing with ranges

ruby · try it yourself
text = "Programming"
p text[0..2]
p text[3...7]
p text[-4..]

nums = [10, 20, 30, 40, 50]
p nums[1..3]
nums[1..2] = [0]
p nums
"Pro"
"gram"
"ming"
[20, 30, 40]
[10, 0, 40, 50]

Range methods

ruby · try it yourself
r = 3..9
p r.first, r.last
p r.first(2)
p r.min, r.max
p r.minmax
p r.to_a.length
p r.step(3).to_a
p (1..10) % 4
p ("aa".."ad").to_a
3
9
[3, 4]
3
9
[3, 9]
7
[3, 6, 9]
((1..10).%(4))
["aa", "ab", "ac", "ad"]

(1..10) % 4 is another way to write (1..10).step(4); it returns an arithmetic sequence object (printed in that unusual form).

Tip: Need a list of dates, letters or numbers? A range plus to_a (or map) is usually the shortest way to build it.

Test yourself

How many numbers are in 1...10?

  1. 10
  2. 9
  3. 11
  4. 1

What does "Hello"[1..] return?

  1. "H"
  2. "ello"
  3. "Hello"
  4. "o"

Which expression is true?

  1. (1..5).include?(5.5)
  2. (1...5).include?(5)
  3. (1..5) === 3
  4. (5..1).to_a == [5, 4, 3, 2, 1]