Bug #6078

The name of a module nested in an anonymous module depends on how it was created

Added by Brian Shirai about 2 years ago. Updated over 1 year ago.

[ruby-core:42865]
Status:Closed
Priority:Normal
Assignee:Nobuyoshi Nakada
Category:-
Target version:-
ruby -v:ruby 1.9.3p125 (2012-02-16 revision 34643) [x86_64-darwin10.8.0] Backport:

Description

If a module is nested under an anonymous module, the nested modules name will depend on how the module is created. See below:

$ irb
1.9.3p125 :001 > m = Module.new
=> #Module:0x000001009e1398
1.9.3p125 :002 > n = Module.new
=> #Module:0x000001009e1ca8
1.9.3p125 :003 > m::N = n
=> #Module:0x000001009e1ca8
1.9.3p125 :004 > m.name
=> nil
1.9.3p125 :005 > n.name
=> nil
1.9.3p125 :006 > m.constants
=> [:N]
1.9.3p125 :007 > module m::O
1.9.3p125 :008?> end
=> nil
1.9.3p125 :009 > m.constants
=> [:N, :O]
1.9.3p125 :010 > m::N.name
=> nil
1.9.3p125 :011 > m::O.name
=> "#Module:0x000001009e1398::O"
1.9.3p125 :012 > m.constget(:N).name
=> nil
1.9.3p125 :013 > m.const
get(:O).name
=> "#Module:0x000001009e1398::O"
1.9.3p125 :014 > M = m
=> M
1.9.3p125 :015 > m::N.name
=> "M::N"
1.9.3p125 :016 > m::O.name
=> "#Module:0x000001009e1398::O"

Thanks,
Brian

Associated revisions

Revision 36574
Added by Nobuyoshi Nakada over 1 year ago

variable.c: tmp_classpath

  • variable.c: store anonymous class path in tmp_classpath but not in classpath. [Bug #6078]

Revision 36577
Added by Nobuyoshi Nakada over 1 year ago

variable.c: fix r36574

  • variable.c (findclasspath): no retry when preferred is given.
  • variable.c (classname): if classid is set try it to find full qualified class path, and then try arbitrary class path. try tmp_classpath at last even if enclosing namespace is anonymous. fix r36574. [Bug #6078]
  • variable.c (rbsetclasspathstring, rbsetclasspath): set tmpclasspath instead of classpath if the name is not permanent.

Revision 36584
Added by Nobuyoshi Nakada over 1 year ago

variable.c: fix r36574

  • variable.c (classname): tell if found name is permanent. search tmp_classpath only if class id is set. [Bug #6078]
  • variable.c (rbclasspath): duplicate found temporary path.
  • variable.c (rbsetclasspathstring, rbsetclass_path): set class id to find classpath.

History

#1 Updated by Brian Shirai about 2 years ago

There is a case not given above where it may make sense to give the class/module a name based on #inspect output of the containing class. This case is when the containing class is a singleton class:

$ irb
1.9.3p125 :001 > obj = Object.new
=> #Object:0x00000101147d30
1.9.3p125 :002 > class << obj
1.9.3p125 :003?> class B
1.9.3p125 :004?> end
1.9.3p125 :005?> end
=> nil
1.9.3p125 :006 > obj.singletonclass.name
=> nil
1.9.3p125 :007 > obj.singleton
class.const_get(:B).name
=> "#Class:0x00000101142948::B"
1.9.3p125 :008 >

However, note that the singleton class itself has no name, so giving the nested class/module a name like "#Class:0x00000101142948::B" seems wrong.

Thanks,
Brian

#2 Updated by Koichi Sasada about 2 years ago

  • Assignee set to Nobuyoshi Nakada

#3 Updated by Shyouhei Urabe about 2 years ago

  • Status changed from Open to Assigned

#4 Updated by Nobuyoshi Nakada over 1 year ago

  • Status changed from Assigned to Closed
  • % Done changed from 0 to 100

This issue was solved with changeset r36574.
Brian, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.


variable.c: tmp_classpath

  • variable.c: store anonymous class path in tmp_classpath but not in classpath. [Bug #6078]

#5 Updated by Nobuyoshi Nakada over 1 year ago

  • % Done changed from 100 to 50

#6 Updated by Nobuyoshi Nakada over 1 year ago

  • % Done changed from 50 to 100

Also available in: Atom PDF