Ruby 简明教程

Ruby - Ranges

范围无所不在:从 1 月到 12 月、从 0 到 9、从第 50 行到第 67 行,等等。Ruby 支持范围,并允许我们以多种方式使用范围 −

Ranges occur everywhere: January to December, 0 to 9, lines 50 through 67, and so on. Ruby supports ranges and allows us to use ranges in a variety of ways −

  1. Ranges as Sequences

  2. Ranges as Conditions

  3. Ranges as Intervals

Ranges as Sequences

第一个也是最自然的范围用法是用来表述序列。序列有起点、终点和生成序列中连续值的方法。

The first and perhaps the most natural use of ranges is to express a sequence. Sequences have a start point, an end point, and a way to produce successive values in the sequence.

Ruby 使用 ''..''''…​'' 范围运算符创建这些序列。两点形式创建一个包括范围,而三点形式创建一个不包括指定高值的范围。

Ruby creates these sequences using the ''..'' and ''…​'' range operators. The two-dot form creates an inclusive range, while the three-dot form creates a range that excludes the specified high value.

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

1..100 序列作为一个范围对象保存,该对象包含对两个 Fixnum 对象的引用。如果需要,您可以使用 to_a 方法将范围转换成列表。尝试以下示例 −

The sequence 1..100 is held as a Range object containing references to two Fixnum objects. If you need to, you can convert a range to a list using the to_a method. Try the following example −

#!/usr/bin/ruby

$, =", "   # Array value separator
range1 = (1..10).to_a
range2 = ('bar'..'bat').to_a

puts "#{range1}"
puts "#{range2}"

这会产生以下结果 −

This will produce the following result −

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

范围实现的方法让您以多种方式对其进行迭代并测试其内容 −

Ranges implement methods that let you iterate over them and test their contents in a variety of ways −

#!/usr/bin/ruby

# Assume a range
digits = 0..9

puts digits.include?(5)
ret = digits.min
puts "Min value is #{ret}"

ret = digits.max
puts "Max value is #{ret}"

ret = digits.reject {|i| i < 5 }
puts "Rejected values are #{ret}"

digits.each do |digit|
   puts "In Loop #{digit}"
end

这会产生以下结果 −

This will produce the following result −

true
Min value is 0
Max value is 9
Rejected values are 5, 6, 7, 8, 9
In Loop 0
In Loop 1
In Loop 2
In Loop 3
In Loop 4
In Loop 5
In Loop 6
In Loop 7
In Loop 8
In Loop 9

Ranges as Conditions

范围也可以用作条件表达式。例如,以下代码片段从标准输入打印出一组行,其中每组中的第一行包含单词 start,最后一行包含单词 ends −

Ranges may also be used as conditional expressions. For example, the following code fragment prints sets of lines from the standard input, where the first line in each set contains the word start and the last line the word ends −

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

范围可以用于 case 语句中 −

Ranges can be used in case statements −

#!/usr/bin/ruby

score = 70

result = case score
   when 0..40 then "Fail"
   when 41..60 then "Pass"
   when 61..70 then "Pass with Merit"
   when 71..100 then "Pass with Distinction"
   else "Invalid Score"
end

puts result

这会产生以下结果 −

This will produce the following result −

Pass with Merit

Ranges as Intervals

多功能范围的最后一种用途是作为区间测试:查看某个值是否落在范围表示的区间内。这是使用 === 即 case 相等运算符来完成的。

A final use of the versatile range is as an interval test: seeing if some value falls within the interval represented by the range. This is done using ===, the case equality operator.

#!/usr/bin/ruby

if ((1..10) === 5)
   puts "5 lies in (1..10)"
end

if (('a'..'j') === 'c')
   puts "c lies in ('a'..'j')"
end

if (('a'..'j') === 'z')
   puts "z lies in ('a'..'j')"
end

这会产生以下结果 −

This will produce the following result −

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