From: "mame (Yusuke Endoh) via ruby-core" <ruby-core@ml.ruby-lang.org>
To: ruby-core@ml.ruby-lang.org
Cc: "mame (Yusuke Endoh)" <noreply@ruby-lang.org>
Subject: [ruby-core:122451] [Ruby Bug#21384] const_added is triggered twice when using autoload
Date: Thu, 05 Jun 2025 11:00:44 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-113632.20250605110044.58511@ruby-lang.org> (raw)
In-Reply-To: <redmine.issue-21384.20250529230200.58511@ruby-lang.org>
Issue #21384 has been updated by mame (Yusuke Endoh).
Briefly discussed at the dev meeting. @matz said that it was not intentional to fire the hook twice. But changing it now would be a compatibility concern, so he decided to keep the current behavior.
Conceptually, we may consider this behavior as:
* When `autoload` is set, the constant is (virtually) defined (and `const_added` fires)
* When the constant is actually defined by the firing of autoload, the constant is conceptually deleted once and defined again (and `const_added` fires again)
----------------------------------------
Bug #21384: const_added is triggered twice when using autoload
https://bugs.ruby-lang.org/issues/21384#change-113632
* Author: petekinnecom (Pete Kinnecom)
* Status: Closed
* Backport: 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
As the title says, I've noticed that `const_added` is invoked twice when using `autoload`. I'm wondering if this behavior is intended or perhaps a bug. Here's a small script to reproduce what I'm seeing:
``` ruby
require "tmpdir"
dir = Dir.mktmpdir
File.write(
File.join(dir, "const.rb"),
"class Const; end"
)
def Object.const_added(const_name)
super.tap { puts "const_added: #{const_name}" }
end
$LOAD_PATH << dir
puts "before autoload call"
autoload :Const, "const"
puts "after autoload call"
puts Const
# Produces output:
#
# => before autoload call
# => const_added: Const
# => after autoload call
# => const_added: Const
# => Const
```
I'm seeing the behavior using the following docker containers ruby:3.2 and ruby:3.5-rc when running like so: `docker run -v ./:/home ruby:3.5-rc ruby /home/script.rb`. Thanks.
--
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/
prev parent reply other threads:[~2025-06-05 11:01 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-05-29 23:02 [ruby-core:122338] " petekinnecom (Pete Kinnecom) via ruby-core
2025-05-30 15:30 ` [ruby-core:122341] " fxn (Xavier Noria) via ruby-core
2025-05-30 15:32 ` [ruby-core:122342] " fxn (Xavier Noria) via ruby-core
2025-05-30 23:56 ` [ruby-core:122348] " petekinnecom (Pete Kinnecom) via ruby-core
2025-06-05 11:00 ` mame (Yusuke Endoh) via ruby-core [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-113632.20250605110044.58511@ruby-lang.org \
--to=ruby-core@ml.ruby-lang.org \
--cc=noreply@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).