From: "mame (Yusuke Endoh) via ruby-core" Date: 2025-04-22T00:46:08+00:00 Subject: [ruby-core:121703] [Ruby Misc#21154] Document or change Module#autoload? Issue #21154 has been updated by mame (Yusuke Endoh). fxn (Xavier Noria) wrote in #note-30: > Correct, Zeitwerk only sets autoloads with absolute paths (that may contain symlinks). Thank you. So, Zeitwerk is able to avoid the limitation clarified in this ticket, right? > > In general, autoload with a relative path needs to resolve the path to absolute one. This resolution may be too costly to do for all autoload calls by default. > > And that is what the predicate `autoload?` is doing, which is the origin of this ticket. Please note that the performance of `Kernel#autoload?` is not considered important. The purpose of `autoload` is to speed up boot time by deferring `require`. One typical use case involves configuring many `autoload`s but exiting the program without actually triggering most of them. Therefore, the performance of `Kernel#autoload` is important. On the other hand, metaprogramming features like `Kernel#autoload?` are not expected to require high performance. > I don't even know if that is intentional or it is implementation leaking The current behavior of `autoload?` and `const_defined?` is intentional. These methods return truthy values when the constant is considered available in the current context. ```ruby # main.rb autoload :Foo, "./foo.rb" # In the context between the autoload setup and the point when it is triggered, # the constant Foo is considered (virtually) available. # So autoload? and const_defined? return truthy values. p autoload?(:Foo) #=> "./foo.rb" p Object.const_defined?(:Foo) #=> true # Trigger the autoload Foo ``` ```ruby # foo.rb # In the context between triggering the autoload and the actual definition of the constant, # the constant Foo is not considered available. # So autoload? and const_defined? return falsy values. p autoload?(:Foo) #=> nil p Object.const_defined?(:Foo) #=> false module Foo end ``` To achieve this behavior, `Kernel#autoload?` checks whether the autoload for the corresponding constant has already been triggered. In the current implementation, this check is effectively done by seeing whether the associated `require` has been called or not. ---------------------------------------- Misc #21154: Document or change Module#autoload? https://bugs.ruby-lang.org/issues/21154#change-112756 * Author: fxn (Xavier Noria) * Status: Feedback * Assignee: mame (Yusuke Endoh) ---------------------------------------- The documentation of `Module#autoload?` says > Returns filename to be loaded if name is registered as autoload in the namespace of mod or one of its ancestors. Cool, but in the following snippet ```ruby autoload :Foo, 'foo' autoload?(:Foo) ``` the second line could evaluate to `nil`, and this does not seem to agree. I just registered an autoload, therefore (according to the documentation) I should get "foo" back in line 2. I'd like to ask for clarification from the Ruby team: 1. Is the documentation complete? Should that second line always return "foo"? 2. If the answer is no, which is the logic missing in the docs? Thank you! -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/