[ruby-core:76043] Re: [Ruby trunk Feature#12317] Name space of a module
From:
Daniel Ferreira <danieldasilvaferreira@...>
Date:
2016-06-15 21:26:42 UTC
List:
ruby-core #76043
From what I can understand this is what we actually have with that
challenge code:
~~~ ruby
module A; module B; end; end
module C
Z = 2
module A::B
def self.foo
Z
end
end
end
::C::Z #=> 2
::A::B.foo #=> 2
~~~
Which will be the same as:
~~~ ruby
module ::A; module B; end; end
module ::C
Z = 2
end
module ::A::B
def self.foo
::C::Z
end
end
::C::Z #=> 2
::A::B.foo #=> 2
~~~
hence the namespace method should return:
~~~ ruby
::A::B.namespace #=> [A::B, A]
~~~
With the root of the namespace being:
~~~ ruby
::A::B.namespace.last #=> A
~~~
Now we have the `ancestors` method returning:
~~~ ruby
::A::B.ancestors #=> [A::B]
~~~
Currently we have the following error:
~~~ ruby
::C::A::B.ancestors
#=> NameError: uninitialized constant C::A
# I guess we should return the same error for any method call on that
context
~~~
This code was replicated in irb under ruby 2.3.0
On Wed, Jun 15, 2016 at 5:55 AM, <shyouhei@ruby-lang.org> wrote:
> Issue #12317 has been updated by Shyouhei Urabe.
>
>
> "What is a namespace?" can be a fundamental issue here. I guess Akira's
> point at comment #6 is that a "namespace" is to do something with names.
> From such point of view, the "A::B" output is at least insufficient to be a
> "namespace". That cannot explain why Z is visible inside.
>
> ----------------------------------------
> Feature #12317: Name space of a module
> https://bugs.ruby-lang.org/issues/12317#change-59231
>
> * 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>
>
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>