From: "fxn (Xavier Noria) via ruby-core" Date: 2025-04-15T09:56:31+00:00 Subject: [ruby-core:121667] [Ruby Misc#21154] Document or change Module#autoload? Issue #21154 has been updated by fxn (Xavier Noria). Perhaps that would work for this exact use case. But it would be ad-hoc and limited, it would not be addressing the conflict at its root cause. As documented, `for_gem` is only a convenience shortcut for a common use case. Gems can use the generic API, instantiate the loader in arbitrary places, and some do. And I have more use cases, like a gem that migrates to Zeitwerk and existing client code has `require` calls for files inside the gem. Nowadays, those calls work. All of this works today, [it even has test coverage](https://github.com/fxn/zeitwerk/blob/67b1396cab1bb517b9a63833417831d648794fcc/test/lib/zeitwerk/test_ruby_compatibility.rb#L195-L240) for other interpreters. If the change was: "If the autoload is circular, it is ignored and the return value says so", I _believe_ I could modify the gem to take that into account, provided that nowadays I actually have that logic by asking `autoload?` after it. But it was discarded in a previous comment as "too costly" (from memory). But, the key point is you cannot honor what the docs say in `master`. I suspect they have static usage in mind, but are not considering dynamic usage where, as far as I know, you do not have the tools to check if an arbitrary file is being loaded at the moment, so you cannot now if you can set an autoload, and strictly speaking then you cannot set ANY autoload. Let's do this experiment: 1. I'd like to set an autoload for this file dynamically. 2. As per the docs, you cannot do that if the file is being loaded. 3. How can I check that? 4. You cannot (unless I am missing some API). 5. Therefore, I cannot set the autoload. ---------------------------------------- Misc #21154: Document or change Module#autoload? https://bugs.ruby-lang.org/issues/21154#change-112721 * 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/