[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Dynamically determining a class' namespace

Daniel Berger

2/13/2007 8:29:00 PM

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

4 Answers

Ara.T.Howard

2/13/2007 8:56:00 PM

0

Tim Pease

2/13/2007 9:21:00 PM

0

On 2/13/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Wed, 14 Feb 2007, Daniel Berger wrote:
>
> > 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
> >
> >
>
> http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb42...
>

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

Tim Pease

2/13/2007 9:52:00 PM

0

On 2/13/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Wed, 14 Feb 2007, Tim Pease wrote:
>
> > On 2/13/07, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> >> On Wed, 14 Feb 2007, Daniel Berger wrote:
> >>
> >> > 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
> >> >
> >> >
> >>
> >> http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/964c11aefa515b29/c1fb423d3dcda20e?lnk=gst&q=a.rb+nesting&rnum=2&hl=en#c1fb42...
> >>
> >
> > 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.
> >
>
> harp:~ > cat a.rb
> require 'binding_of_caller'
>

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

TwP

Ara.T.Howard

2/13/2007 10:03:00 PM

0