當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Ruby Range類用法及代碼示例

本文簡要介紹ruby語言中 Range類 的用法。

Range 對象表示介於給定開始值和結束值之間的值的集合。

您可以使用以下命令顯式創建 Range 對象:

  • 一個range literal

    # Ranges that use '..' to include the given end value.
    (1..4).to_a      # => [1, 2, 3, 4]
    ('a'..'d').to_a  # => ["a", "b", "c", "d"]
    # Ranges that use '...' to exclude the given end value.
    (1...4).to_a     # => [1, 2, 3]
    ('a'...'d').to_a # => ["a", "b", "c"]

可以使用方法 Range.new 創建範圍:

# Ranges that by default include the given end value.
Range.new(1, 4).to_a     # => [1, 2, 3, 4]
Range.new('a', 'd').to_a # => ["a", "b", "c", "d"]
# Ranges that use third argument +exclude_end+ to exclude the given end value.
Range.new(1, 4, true).to_a     # => [1, 2, 3]
Range.new('a', 'd', true).to_a # => ["a", "b", "c"]

無起點範圍

beginless range 具有確定的結束值,但 nil 開始值。這樣的範圍包括直到最終值的所有值。

r = (..4)               # => nil..4
r.begin                 # => nil
r.include?(-50)         # => true
r.include?(4)           # => true

r = (...4)              # => nil...4
r.include?(4)           # => false

Range.new(nil, 4)       # => nil..4
Range.new(nil, 4, true) # => nil...4

無起始範圍可用於對數組進行切片:

a = [1, 2, 3, 4]
r = (..2) # => nil...2
a[r]      # => [1, 2]

用於無開始範圍的方法 each 會引發異常。

無盡的範圍

endless range 有一個明確的開始值,但有一個 nil 結束值。這樣的範圍包括從開始值開始的所有值。

r = (1..)         # => 1..
r.end             # => nil
r.include?(50)    # => true

Range.new(1, nil) # => 1..

無限範圍的文字可以用兩個點或三個點來書寫。無論哪種方式,該範圍都具有相同的元素。但請注意,兩者不相等:

r0 = (1..)           # => 1..
r1 = (1...)          # => 1...
r0.begin == r1.begin # => true
r0.end == r1.end     # => true
r0 == r1             # => false

無限範圍可用於對數組進行切片:

a = [1, 2, 3, 4]
r = (2..) # => 2..
a[r]      # => [3, 4]

無限範圍的方法 each 無限期地調用給定塊:

a = []
r = (1..)
r.each do |i|
  a.push(i) if i.even?
  break if i > 10
end
a # => [2, 4, 6, 8, 10]

範圍和其他類

如果對象的類實現實例方法 <=> ,則可以將對象放入範圍中。這樣做的 Ruby 核心類包括 Array , Complex , File::Stat , Float , Integer , Kernel , Module , Numeric , Rational , String , Symbol Time

例子:

t0 = Time.now         # => 2021-09-19 09:22:48.4854986 -0500
t1 = Time.now         # => 2021-09-19 09:22:56.0365079 -0500
t2 = Time.now         # => 2021-09-19 09:23:08.5263283 -0500
(t0..t2).include?(t1) # => true
(t0..t1).include?(t2) # => false

隻有當其元素實現實例方法 succ 時,才能迭代範圍。這樣做的 Ruby 核心類包括 Integer String Symbol (但不包括上麵提到的其他類)。

迭代器方法包括:

例子:

a = []
(1..4).each {|i| a.push(i) }
a # => [1, 2, 3, 4]

範圍和用戶定義的類

要在範圍中使用的用戶定義類必須實現實例 <=> ;見Integer#。為了使迭代可用,它還必須實現實例方法succ;見 Integer#succ

下麵的類同時實現了 <=>succ ,因此可以用於構造範圍和迭代它們。請注意,包含 Comparable 模塊,因此 == 方法是根據 <=> 定義的。

# Represent a string of 'X' characters.
class Xs
  include Comparable
  attr_accessor :length
  def initialize(n)
    @length = n
  end
  def succ
    Xs.new(@length + 1)
  end
  def <=>(other)
    @length <=> other.length
  end
  def to_s
    sprintf "%2d #{inspect}", @length
  end
  def inspect
    'X' * @length
  end
end

r = Xs.new(3)..Xs.new(6) #=> XXX..XXXXXX
r.to_a                   #=> [XXX, XXXX, XXXXX, XXXXXX]
r.include?(Xs.new(5))    #=> true
r.include?(Xs.new(7))    #=> false

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Range類。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。