# Dynamically determining a class' namespace

**URL:** <https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467>\
**Category:** Ruby\
**Created:** [February 13, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467 "2007-02-13T21:31:50Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 9:31pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/1 "2007-02-13T21:31:50Z")

</div>

Hi all,

Ruby 1.8.x

Is there a way to determine a classes namespace from within an  
instance method?

class Foo  
def initialize  
# print namespace  
end  
end

Foo.new # ‘Object’? ‘Kernel’? nil?

module Bar  
Foo.new # ‘Bar’  
end

module Baz  
module Bar  
Foo.new # ‘Baz::Bar’  
end  
end

If not, is it possible at the class level? I looked at Module#nesting  
and Module#name, but I couldn’t see how to use those to get what I  
want.

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 9:56pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/2 "2007-02-13T21:56:54Z")

</div>

On Wed, 14 Feb 2007, Daniel B. wrote:

> end  
> Foo.new # ‘Baz::Bar’

[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb423d3dcda20e](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb423d3dcda20e)

-a

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/3 "2007-02-13T22:21:59Z")

</div>

On 2/13/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def initialize  
> > module Baz
> > 
> > Dan
> 
> [http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb423d3dcda20e](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb423d3dcda20e)

Not quite what Mr. Berger is after here.

He wants to know the nesting where the Foo object was created not the  
nesting of the Foo class itself. i.e. whenever Foo.new is called,  
what is the namespace where it was called.

Frankly, I’m stumped.

TwP

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/4 "2007-02-13T22:40:00Z")

</div>

On Wed, 14 Feb 2007, Tim P. wrote:

> > > class Foo
> > > 
> > > Thanks,  
> > > He wants to know the nesting where the Foo object was created not the  
> > > nesting of the Foo class itself. i.e. whenever Foo.new is called,  
> > > what is the namespace where it was called.
> 
> Frankly, I’m stumped.

harp:~ \> cat a.rb  
require ‘binding\_of\_caller’

class Foo  
def self.new  
Binding.of\_caller{|b| p eval(‘Module.nesting’, b).shift }  
end  
end

Foo.new # ‘Object’? ‘Kernel’? nil?

module Bar  
Foo.new # ‘Bar’  
end

module Baz  
module Bar  
Foo.new # ‘Baz::Bar’  
end  
end

harp:~ \> ruby a.rb  
nil  
Bar  
Baz::Bar

-a

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 11:04pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/5 "2007-02-13T23:04:02Z")

</div>

On Wed, 14 Feb 2007, Tim P. wrote:

> > require ‘binding\_of\_caller’
> 
> Okay, Ara is there any problem that you have not already solved 😉
> 
> TwP

gotta give credit where it’s due: that’s florian’s work.

-a

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 13, 2007, 10:53pm UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/6 "2007-02-13T22:53:01Z")

</div>

On 2/13/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > instance method?  
> > > > Foo.new # ‘Bar’  
> > > > want.  
> > > > Not quite what Mr. Berger is after here.

Okay, Ara is there any problem that you have not already solved 😉

TwP

---

<div class="post-metadata">

**Author:** ![danielb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c57346/32.png) [@danielb](https://www.ruby-forum.com/u/danielb)\
**Post date:** [February 14, 2007, 12:38am UTC](https://www.ruby-forum.com/t/dynamically-determining-a-class-namespace/85467/7 "2007-02-14T00:38:13Z")

</div>

On Wed, Feb 14, 2007 at 06:39:26AM +0900, removed\_email\_address@domain.invalid wrote:

> ```
> Binding.of_caller{|b| p eval('Module.nesting', b).shift }
> module Bar
> Foo.new # 'Baz::Bar'
> end
> 
> ```
> 
> end
> 
> harp:~ \> ruby a.rb  
> nil  
> Bar  
> Baz::Bar

$ ruby -v a.rb  
ruby 1.8.5 (2006-08-25) [i686-linux]  
nil  
nil  
nil

(Foo with 1.8.5-p12)

1.8.5 killed Binding.of\_caller, take a look at ruby-debug  
[http://rubyforge.org/projects/ruby-debug/](http://rubyforge.org/projects/ruby-debug/).
