[ruby-core:111453] [Ruby master Bug#19270] Constants lookup and a singleton class issue

Issue #19270 has been reported by andrykonchin (Andrew Konchin). ---------------------------------------- Bug #19270: Constants lookup and a singleton class issue https://bugs.ruby-lang.org/issues/19270 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed that a constant declared in a singleton class may be not visible on an object: ```ruby class A def c; C; end end a = A.new klass = (class << a; self; end) klass.const_set(:C, 1) a.c # (irb):2:in `c': uninitialized constant A::C (NameError) ``` I would expect that such constant is visible and accessible on an object. It is expected and intentional behaviour? -- https://bugs.ruby-lang.org/

Issue #19270 has been updated by alanwu (Alan Wu). It's expected behavior. When you have a singleton class `singleton_class.ancestors` is: [singleton_class, attached_object_class, ...] In this case the lookup for starts at `A`, which is `attached_object_class`. It goes to the right so it never searches `singleton_class`. The lookup resolves when the constant is on `attached_object_class` and it starts at `singleton_class`: ```ruby class A C = 1 end a = A.new klass = (class << a; self; end) p klass::C # => 1 ``` ---------------------------------------- Bug #19270: Constants lookup and a singleton class issue https://bugs.ruby-lang.org/issues/19270#change-100876 * Author: andrykonchin (Andrew Konchin) * Status: Open * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed that a constant declared in a singleton class may be not visible on an object: ```ruby class A def c; C; end end a = A.new klass = (class << a; self; end) klass.const_set(:C, 1) a.c # (irb):2:in `c': uninitialized constant A::C (NameError) ``` I would expect that such constant is visible and accessible on an object. It is expected and intentional behaviour? -- https://bugs.ruby-lang.org/

Issue #19270 has been updated by Eregon (Benoit Daloze). Status changed from Open to Closed @alanwu By `attached_object_class` you mean `a.class` so `A`? Constant lookup behaves lexically, here the scopes around method `c` are `A` and top-level/Object. So here it looks in `A`, then `Object`, then ancestors of A. It never looks in subclasses of A (and `klass` is a subclass of A). ---------------------------------------- Bug #19270: Constants lookup and a singleton class issue https://bugs.ruby-lang.org/issues/19270#change-100992 * Author: andrykonchin (Andrew Konchin) * Status: Closed * Priority: Normal * ruby -v: 3.1.3 * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- I've noticed that a constant declared in a singleton class may be not visible on an object: ```ruby class A def c; C; end end a = A.new klass = (class << a; self; end) klass.const_set(:C, 1) a.c # (irb):2:in `c': uninitialized constant A::C (NameError) ``` I would expect that such constant is visible and accessible on an object. It is expected and intentional behaviour? -- https://bugs.ruby-lang.org/
participants (3)
-
alanwu (Alan Wu)
-
andrykonchin (Andrew Konchin)
-
Eregon (Benoit Daloze)