From: sawadatsuyoshi@...
Date: 2016-06-13T10:17:56+00:00
Subject: [ruby-core:75991] [Ruby trunk Feature#12317] Name space of a module

Issue #12317 has been updated by Tsuyoshi Sawada.


Akira Tanaka wrote:
> I think lexical information is not obtainable in a method.
> 
> ```
> % ruby -e '
> module A
>   module B
>   end
> end
> 
> module C
>   Z = 2
>   module A::B
>     p Z
>   end
> end
> 
> p A::B.namespaces
> '
> 2
> ???
> ```
> 
> Z is refarenceable in B in C but not B in C.
> Since B in C and B in A is same object, A::B.namespaces method cannot
> distinguish them.

I am not sure if I get the issue correctly. I expect:

~~~ruby
module A; module B; end end
module C
  A::B # => A::B
end
A::B.namespace # => [A::B, A]
C::A::B.namespace # => [A::B, A]
~~~

----------------------------------------
Feature #12317: Name space of a module
https://bugs.ruby-lang.org/issues/12317#change-59187

* Author: Tsuyoshi Sawada
* Status: Open
* Priority: Normal
* Assignee: 
----------------------------------------
I want a method to return the  name space of a module, something like:

~~~ruby
class A; module B; module C end end end

A::B::C.namespace => [A, A::B, A::B::C]
~~~

There is `nesting` method that is similar, but that only returns the lexical nesting information.

There are also some known hacks for this, converting the module to the string representation using `to_s` or `name`, and then splitting it by `::`. But that easily breaks if the module is anonymous, or is a singleton module. I would like a more robust, core method.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>