Issue #12317 has been updated by Robert A. Heiler.


Interesting. I am indifferent to it, but possibly lean towards giving a +1.

In some of my classes, I use a module called "opn", which stands short for
"output program name". I needed this for some of my ruby code, when it
gives output on the terminal, to know where exactly the output happens (since
I tend to use a lot of different scripts, in different projects).

Perhaps a year or so ago, I added a new method called "opnn". That name is
not really logical, but I simply repeat the last character, which helps
me identify that I still want to call "opn", but I will do so including
the namespace.

This may sound confusing, so here is what happens in ruby code:

    class Foo

    class Bar

      def initialize
        opnn; e 'Hello World!'
      end

    end

    end

    Foo::Bar.new

And the above will output Hello World! (e is my alias for puts, I am
lazy, ruby allows me to be lazy) in grey ansi colours, prefixed with
the name of the class in question.

So the output will actually be:

Foo::Bar: Hello World!

So far, so good. All works fine.

opnn is a weird method though; I actually define it on the class itself.

The definition tends to be like so:

    def opnn
      Opn.opn(namespace: NAMESPACE)
    end

Opn is obviously a module, namespace, in the gem called opn. The argument
is a hash. The constant called NAMESPACE is actually the namespace, and
now here comes the relevant part.

I define that constant within the class itself like this:

    NAMESPACE = self.inspect.to_s

Which I found has worked best so far.

All of this is not really ... awesome. In particular, that I have to manually
define the namespace for each class, is not so great.

Perhaps a __NAMESPACE__ identifier on that of that, which would be equivalent
to self.inspect.to_s all the time? (Or perhaps some shorter way ... I just
need a simple way to obtain the string representation of the namespace in
question)

This suggestion is a bit different from Tsuyoshi Sawada, I don't want to hijack
his suggestion. I just found it semi-fitting if I also detail a bit how I 
write ruby code and deal with namespaces (and this may all change as time passes
by and better ways are found)

Thanks!

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

* 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>