Bug #21317
closedNamespaces leak with object IDs
Description
Namespaces are not transparent for this program
if mod1.object_id == mod2.object_id
mod1.constants == mod2.constants
end
because it may fail for mod1 = Object; mod2 = ns::Object
, which have the same object ID.
Updated by mame (Yusuke Endoh) about 18 hours ago
Did you actually test the code? mod1.constants == mod2.constants
returns true
.
$ cat main.rb
ns = Namespace.new
mod1 = Object
mod2 = ns::Object
p mod1 #=> Object
p mod2 #=> Object
p mod1.object_id == mod2.object_id #=> true
p mod1.constants == mod2.constants #=> true
$ RUBY_NAMESPACE=1 ./miniruby main.rb
./miniruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for know issues, etc.
Object
Object
true
true
It's great that you're splitting the ticket and discussing each topic in detail. Thank you for that.
That said, when creating tickets, please make sure to include the test code, what you expected to happen, and what actually happened, in the usual format.
Updated by fxn (Xavier Noria) about 15 hours ago
I suspect this one was an error of mine. I think we can close.
Updated by fxn (Xavier Noria) about 13 hours ago
@byroot (Jean Boussier), if you will, I believe this one can be closed too.
Updated by byroot (Jean Boussier) about 13 hours ago
- Status changed from Open to Closed
Updated by Eregon (Benoit Daloze) about 12 hours ago
Full reproducer, showing that even though Object
is the same reference in all namespaces it can have different constants in different namespaces, but only if calling constants
in that namespace:
$ RUBY_NAMESPACE=1 ruby -ve 'ns = Namespace.new; File.write "ns.rb", "O = Object; OC = Object.constants"; ns.require "./ns"; p Object.equal?(ns::O); p Object.constants == ns::OC; p Object.constants == ns::Object.constants'
ruby 3.5.0dev (2025-05-10T07:50:29Z namespace-on-read-.. bd4f57f96b) +PRISM [x86_64-linux]
ruby: warning: Namespace is experimental, and the behavior may change in the future!
See doc/namespace.md for know issues, etc.
true
false
true
I think this is expected behavior for Namespace so not sure it's worth reopening.
It does mean that with Namespace even though two objects (specifically core classes and modules) might be equal they are actually different when looked at in different namespaces.
Updated by Eregon (Benoit Daloze) about 11 hours ago
- Related to Bug #21322: Namespaces and builtin classes as arguments and return values added
Updated by Eregon (Benoit Daloze) about 11 hours ago
Seems very related to https://bugs.ruby-lang.org/issues/21322 so let's keep this one closed and discuss there.