📚

[Bug #21030] <ActiveSupport::Duration> が Range に含まれている場合のバグ報告

2025/02/06に公開

[Bug #21030] Bug: #step with RangeActiveSupport::Duration behavior broken on Ruby 3.4.1

  • Ruby 3.4.1 から ActiveSupport::Duration が含まれる Range の挙動が壊れているというバグ報告
  • 次のように先端と終端が同じ値の Range に対して #step -> #to_a を呼び出した場合に問題があるとのこと
    • (n..n).step.to_a # => [n] を期待しているが [] が返ってくるバグ
require 'active_support/all' # version 7.1.5.1

# これは Ruby 標準の数値
int = 100

# これが ActiveSupport で定義されれている ActiveSupport/Duration オブジェクト
dur = 100.seconds # ActiveSupport/Duration

# [dur] を期待するが [] が返ってくる
p (dur..dur).step(10).to_a
# Ruby 3.3 => [100 seconds]
# Ruby 3.4 => []

# こちらも同様
p (int..dur).step(10).to_a
# Ruby 3.3 => [100 seconds]
# Ruby 3.4 => []

# 以下は 3.3 も 3.4 も挙動は変わらない
p (int..int).step(10).to_a
# Ruby 3.3 => [100]
# Ruby 3.4 => [100]

# こちらも同様
p (int..dur).to_a
# Ruby 3.3 => [100]
# Ruby 3.4 => [100]

# 先端と終端が異なる場合も 3.3 と 3.4 で挙動に際はない
dur2 = 120.seconds
p (dur..dur2).step(10).to_a
# Ruby 3.3 => [100 seconds, 110 seconds, 120 seconds]
# Ruby 3.4 => [100 seconds, 110 seconds, 120 seconds]

p (int..dur2).step(10).to_a
# Ruby 3.3 => [100, 110, 120]
# Ruby 3.4 => [100, 110, 120]
  • Ruby 3.4 で Range#step 周りがいろいろと改修された影響なんですかね?
  • この不具合は開発版の Ruby 3.5-dev で修正済みです
GitHubで編集を提案

Discussion