English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Ranges (Range)

Ranges (Range) are everywhere: a to z, 0 to 9, and so on. Ruby supports ranges and allows us to use ranges in different ways:

  • as a sequence range

  • as a conditional range

  • As an interval range

as a sequence range

the first and most common use of ranges is to express sequences. A sequence has a starting point, an ending point, and a way to produce continuous values in the sequence.

Ruby uses ''..'' and ''...'' Range operators create these sequences. The two-point form creates a range that includes the specified highest value, and the three-point form creates a range that does not include the specified highest value.

(1..5)  #==> 1, 2, 3, 4, 5
(1...5)  #==> 1, 2, 3, 4
('a'..'d')  #==> 'a', 'b', 'c', 'd'

sequence 1..100 is a Range objects, which contain two Fixnum the reference of an object. If necessary, you can use to_a methods convert ranges to lists. Try the following example:

Online Examples

#!/usr/bin/ruby
 
$, =", "		# Array value separator
range1 =1..10).to_a
range2 = ('bar'..'bat').to_a
 
puts "#{range1"
puts "#{range2"

The output result of the above example is:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
["bar", "bas", "bat"]

Ranges implement methods that allow you to iterate over them, and you can check their contents in various ways:

Online Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
 
# Specify range
digits = 0..9
 
puts digits.include?(5)
ret = digits.min
puts "The minimum value is #{ret}"
 
ret = digits.max
puts "The maximum value is #{ret}"
 
ret = digits.reject { |i| i < 5 }
puts "The ones that do not meet the condition are #{ret}"
 
digits.each do |digit|
   puts "in a loop #{digit}"
end

The output result of the above example is:

true
The minimum value is 0
The maximum value is 9
The ones that do not meet the condition are [5, 6, 7, 8, 9]
in a loop 0
in a loop 1
in a loop 2
in a loop 3
in a loop 4
in a loop 5
in a loop 6
in a loop 7
in a loop 8
in a loop 9

as a conditional range

Ranges can also be used as conditional expressions. For example, the following code snippet prints lines from standard input, where the first line of each set contains the word startThe last line contains the word end.:

while gets
   print if /start/../end/
end

Ranges can be used in case statements:

Online Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
 
score = 70
 
result = case score
when 0..40
    "Poor Score"
when 41..60
    "Almost Passing"
when 61..70
    "Passing Score"
when 71..100
       "Good Score"
else
    "Incorrect Score"
end
 
puts result

The output result of the above example is:

Passing Score

As an interval range

The last use of the range is for interval detection: check if the specified value is within the specified range. The === equality operator must be used to complete the calculation.

Online Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
 
if ((1..10) ==== 5)
  puts ""5 in (1..10)"
end
 
if (('a'..'j') === 'c')
  puts "c in ('a'..'j')"
end
 
if (('a'..'j') === 'z')
  puts "z in ('a'..'j')"
end

The output result of the above example is:

5 in (1..10)
c in ('a'..'j')