[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Getting an object's class definition namespace

Martin Boese

7/17/2008 8:36:00 AM

In a program I module_eval class definitions into a dynamically created
namespace. To run support code later on I need to know the module it was
defined in from an instance of the class.

Is there any _NICE_ way to replace this 'cheat version'? The below also
doesn't work with nested modules.

module X
class A
def my_module
Object::const_get(self.class.name.split('::')[0])
end
end
end

a = X::A.new
a.my_module => X

Thanks!
martin

2 Answers

James Coglan

7/17/2008 9:02:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

2008/7/17 Martin Boese <boesemar@gmx.de>:

> In a program I module_eval class definitions into a dynamically created
> namespace. To run support code later on I need to know the module it was
> defined in from an instance of the class.



Would Module.nesting work for you?

irb(main):001:0> module X
irb(main):002:1> class A
irb(main):003:2> def nesting
irb(main):004:3> Module.nesting
irb(main):005:3> end
irb(main):006:2> end
irb(main):007:1> end
=> nil
irb(main):008:0> X::A.new.nesting
=> [X::A, X]

Martin Boese

7/17/2008 9:10:00 AM

0

On Thursday 17 July 2008 10:02:21 James Coglan wrote:
> Would Module.nesting work for you?

Yes! Thanks.