このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

Ruby 3.1 リファレンスマニュアル

instance method Enumerator::ArithmeticSequence#end

end -> Numeric | nilRuby 2.6.0 から[permalink][rdoc][edit]

数列の終端の値を返します。終端がない場合は nil を返します。

自身は begin を初項として step ずつ加算して要素を生成しますが、 end はあくまで元になった範囲の終端の値であり、 数列が実際に生成する最後の要素と一致するとは限りません。 step の刻み幅によって end にちょうど到達しない場合や、 Enumerator::ArithmeticSequence#exclude_end? が真で end 自体が数列から除外される場合があるためです。

例: end と実際の最後の要素が一致する場合
s = (1..10).step(3)
p s.end        # => 10
p s.to_a       # => [1, 4, 7, 10]
例: step が end にちょうど到達しない場合
s = (1..10).step(4)
p s.end        # => 10
p s.to_a       # => [1, 5, 9]
例: exclude_end? が真で end が除外される場合
s = (1...10).step(3)
p s.end             # => 10
p s.exclude_end?    # => true
p s.to_a            # => [1, 4, 7]

[SEE_ALSO] Enumerator::ArithmeticSequence#begin

[SEE_ALSO] Enumerator::ArithmeticSequence#step

[SEE_ALSO] Enumerator::ArithmeticSequence#exclude_end?

[SEE_ALSO] Enumerator::ArithmeticSequence#last