🔖
[Bug #21314] autoload が設定されている場合に奇妙な挙動になるというバグ報告
[Bug #21314] Kernel#autoload requires the wrong file (?)
- 以下のような
autoload
が設定されている場合に奇妙な挙動になるというバグ報告
# main.rb
autoload :A, "./a"
autoload :B, "./b"
p A # fires the autoload of A
# a.rb
p :A1
B # fires the autoload of B
class A
end
# b.rb
p :B1
class B
p :B2
p A # expected: requires no file (because a.rb is already being require'ed), and raises a NameError
# actual: the autoload of B is fired (not A!), recursively (not no-op!)
p :B3
end
$ ruby main.rb
:A1
:B1
:B2
:B1 # What?
:B2
:A1
A
:B3
b.rb:17:in '<class:B>': uninitialized constant B::A (NameError)
- 本来であれば
b.rb
でA
を参照するときにa.rb
はrequire
されず定数A
も定義されていないのでNameError
になることを期待する - が、実際には
a.rb
が再帰的に読み込まれて意図しないタイミングでエラーになってしまう - 報告だけで特に進展はしていないみたいですね
Discussion