# Get namespaces

**URL:** https://rubytalk.org/t/get-namespaces/73873
**Category:** ruby-talk
**Created:** [4 November 2016 14:52 UTC](https://rubytalk.org/t/get-namespaces/73873 "2016-11-04T14:52:34Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)
#### Post date: [4 November 2016 14:52 UTC](https://rubytalk.org/t/get-namespaces/73873/1 "2016-11-04T14:52:34Z")

</div>

Hi

Is there a method to get all namespaces (like Math::, which don't have to be loaded necessarily)

Opti

---

<div class="post-metadata">

### Author: ![Rob\_Biedenharn](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/rob_biedenharn/32/1859_2.png) [@Rob\_Biedenharn](https://rubytalk.org/u/Rob_Biedenharn)
#### Post date: [4 November 2016 15:11 UTC](https://rubytalk.org/t/get-namespaces/73873/2 "2016-11-04T15:11:37Z")

</div>

Hi

Is there a method to get all namespaces (like Math::, which don't have  
to be loaded necessarily)

Opti

Unsubscribe: \<[mailto:ruby-talk-request@ruby-lang.org](mailto:ruby-talk-request@ruby-lang.org)?subject=unsubscribe\>  
\<[http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk](http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk)\>

Perhaps something like:

Object.constants.map{|c| eval c.to\_s }.select{|o| o.is\_a?(Module) && !  
o.is\_a?(Class) }

Would be a start.

-Rob

> **···**
>
> On Fri, Nov 4, 2016 at 10:52 AM Die Optimisten \<[inform@die-optimisten.net](mailto:inform@die-optimisten.net)\> wrote:

---

<div class="post-metadata">

### Author: ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)
#### Post date: [4 November 2016 15:15 UTC](https://rubytalk.org/t/get-namespaces/73873/3 "2016-11-04T15:15:10Z")

</div>

which don't have to be loaded necessarily

> 

What do you mean there? And what is the program supposed to have loaded at  
that point?

> **···**
>
> On Fri, Nov 4, 2016 at 3:52 PM, Die Optimisten \<inform@die-optimisten.net\> wrote:

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [4 November 2016 16:34 UTC](https://rubytalk.org/t/get-namespaces/73873/4 "2016-11-04T16:34:23Z")

</div>

I think that requirement is impossible to fulfill since with  
autoloading and other features you cannot know what files can be  
loaded potentially to determine all possible namespaces.

You can determine all currently known namespaces though:

puts ObjectSpace.each\_object(Module).to\_a  
puts ObjectSpace.each\_object(Module).sort\_by {|x| x.name || ""}

Kind regards

robert

> **···**
>
> On Fri, Nov 4, 2016 at 4:15 PM, Xavier Noria \<fxn@hashref.com\> wrote:
> 
> > On Fri, Nov 4, 2016 at 3:52 PM, Die Optimisten \<inform@die-optimisten.net\> \> wrote:
> > 
> > > which don't have to be loaded necessarily
> > 
> > What do you mean there? And what is the program supposed to have loaded at  
> > that point?
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)

---

<div class="post-metadata">

### Author: ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)
#### Post date: [4 November 2016 22:04 UTC](https://rubytalk.org/t/get-namespaces/73873/5 "2016-11-04T22:04:45Z")

</div>

Thank you Robert!  
What Modul is 'fatal ' (lowercase??)

Opti

---

<div class="post-metadata">

### Author: ![Die\_Optimisten](https://avatars.discourse-cdn.com/v4/letter/d/f04885/32.png) [@Die\_Optimisten](https://rubytalk.org/u/Die_Optimisten)
#### Post date: [4 November 2016 23:03 UTC](https://rubytalk.org/t/get-namespaces/73873/6 "2016-11-04T23:03:56Z")

</div>

Hi  
How can I select the main-class, if I have more Modules using the same name for a class?

e.g.:  
String

Neither "abc".A:🅱:String.to\_s nor  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"abc".Main::String.to\_s is working!

Thank you  
Opti

> **···**
>
> A:🅱:String

---

<div class="post-metadata">

### Author: ![Xavier\_Noria](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/xavier_noria/32/1867_2.png) [@Xavier\_Noria](https://rubytalk.org/u/Xavier_Noria)
#### Post date: [4 November 2016 23:32 UTC](https://rubytalk.org/t/get-namespaces/73873/7 "2016-11-04T23:32:23Z")

</div>

Maybe I have missed some emails... how do you know the OP is talking about  
autoloading?

With the original email, I don't really know which is the situation.

> **···**
>
> On Friday, 4 November 2016, Robert Klemme \<shortcutter@googlemail.com\> wrote:
> 
> > On Fri, Nov 4, 2016 at 4:15 PM, Xavier Noria \<fxn@hashref.com \> \<javascript:;\>\> wrote:  
> > \> On Fri, Nov 4, 2016 at 3:52 PM, Die Optimisten \< \> inform@die-optimisten.net \<javascript:;\>\> \> \> wrote:  
> > \>  
> > \>\> which don't have to be loaded necessarily  
> > \>  
> > \> What do you mean there? And what is the program supposed to have loaded  
> > at  
> > \> that point?
> > 
> > I think that requirement is impossible to fulfill since with  
> > autoloading and other features you cannot know what files can be  
> > loaded potentially to determine all possible namespaces.
> 
> --  
> Sent from Gmail Mobile

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [5 November 2016 11:15 UTC](https://rubytalk.org/t/get-namespaces/73873/8 "2016-11-05T11:15:45Z")

</div>

> > \>  
> > \>\> which don't have to be loaded necessarily  
> > \>  
> > \> What do you mean there? And what is the program supposed to have loaded  
> > \> at that point?
> > 
> > I think that requirement is impossible to fulfill since with  
> > autoloading and other features you cannot know what files can be  
> > loaded potentially to determine all possible namespaces.
> 
> Maybe I have missed some emails... how do you know the OP is talking about  
> autoloading?

He was not explicitly. But please look at his sentence I quoted from  
the initial question. He is obviously asking for a general mechanism  
to obtain all namespaces and explicitly states that he wants also  
namespaces which have not been loaded. Autoloading is one mechanism  
which might lead to files loaded at an arbitrary point of time.  
require is another one and eval yet another one.

> With the original email, I don't really know which is the situation.

There are arbitrary ways that classes and modules can come into  
existence at an arbitrary point in time. You could also mention eval.  
Thing is, it is fairly easy to get the \_currently known\_ set of  
classes and modules but there is no way to look into the future  
because of Ruby's dynamic nature.

Kind regards

robert

> **···**
>
> On Sat, Nov 5, 2016 at 12:32 AM, Xavier Noria \<fxn@hashref.com\> wrote:
> 
> > On Friday, 4 November 2016, Robert Klemme \<shortcutter@googlemail.com\> \> wrote:
> > 
> > > On Fri, Nov 4, 2016 at 4:15 PM, Xavier Noria \<fxn@hashref.com\> wrote:  
> > > \> On Fri, Nov 4, 2016 at 3:52 PM, Die Optimisten \>\> \> \<inform@die-optimisten.net\> \>\> \> wrote:
> 
> --  
> [guy, jim, charlie].each {|him| remember.him do |as, often| as.you\_can  
> - without end}  
> [http://blog.rubybestpractices.com/](http://blog.rubybestpractices.com/)
