Module#const_defined?(String) executes autoloads
The documentation of
Module#const_defined? says that autoloads are not executed:
[...] if the constant is not present but there is an autoload for it, true is returned directly without autoloading.
While this seems to work as documented for symbols, constant names passed as strings trigger the autoload:
$ cat x.rb p X = 1 $ cat foo.rb Object.autoload("X", "x") p Object.const_defined?("X") $ ruby -I. foo.rb 1 true
Updated by Eregon (Benoit Daloze) about 1 year ago
I can confirm the bug. Here is a single-line reproducer:
$ ruby -e 'autoload :X, "foo"; p Object.const_defined?("X")' Traceback (most recent call last): 3: from -e:1:in `<main>' 2: from -e:1:in `const_defined?' 1: from /home/eregon/.rubies/ruby-2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require' /home/eregon/.rubies/ruby-2.6.2/lib/ruby/2.6.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- foo (LoadError)
const_defined?("A::B") needs to resolve A to know if it has a B constant, but the last component (B or X) should not trigger any autoload.