Dynamically determining a class' namespace

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#c1fb423d3dcda20e

-a

···

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

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

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

···

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#c1fb423d3dcda20e

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

binding_of_caller.rb (2.2 KB)

···

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#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.

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama

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

TwP

···

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#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.
>

   harp:~ > cat a.rb
   require 'binding_of_caller'

$ 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/\.

···

On Wed, Feb 14, 2007 at 06:39:26AM +0900, ara.t.howard@noaa.gov wrote:

On Wed, 14 Feb 2007, Tim Pease wrote:
>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.
>
  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

--
Mauricio Fernandez - http://eigenclass.org - singular Ruby
                        ** Latest postings **
Ruby, SEX and STDs: infectious practices
  http://eigenclass.org/hiki.rb?ruby-warnings-SEX-and-stds
What's new in Ruby 1.9, Feb. 07 update
  http://eigenclass.org/hiki.rb?Changes-in-Ruby-1.9-update-6

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

-a

···

On Wed, 14 Feb 2007, Tim Pease wrote:

> 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 :wink:

TwP

--
we can deny everything, except that we have the possibility of being better.
simply reflect on that.
- the dalai lama