Project

General

Profile

Actions

Bug #14895

closed

Inconsistent constant names when using const_set on a singleton class

Added by silver_phoenix (Pedro Pinto) almost 6 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.6.0preview2 (2018-05-31 trunk 63539) [x86_64-linux]
[ruby-core:87787]
Tags:

Description

Constants defined in the namespace of a class or a module are named on the fly:

class X; end
X.const_set(:Y, Module.new) # => X::Y
module M; end
M.const_set(:N, Module.new) # => M::N

Constants defined on a singleton class seem to differ. Modules are named only after a module is created through the module keyword.

s = Object.new.singleton_class
s.const_set(:Z, Module.new) # => #<Module:0x000055886ec59a80>
s.class_eval "module A; self end" # => #<Class:0x000055886ec59d00>::A
s.const_set(:B, Module.new) # => #<Class:0x000055886ec59d00>::B

I would expect module Z to be named. For consistency, if module B is named, shouldn't module Z be named as well?

This also happens in these ruby versions:

  • ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  • ruby 2.5.1p57 (2018-03-29 revision 63029) [i386-mingw32]
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0