From: Mark Hubbart Date: 2004-05-29T07:04:55+09:00 Subject: Re: Can an inner class get its container at runtime? On May 28, 2004, at 1:33 PM, Francis Hwang wrote: > Is there a way for an inner class to find out what class owns it? The > code I'd like to use looks like: > > class AbstractInnerClass > def do_something > # here's where I need to find the owner of the concrete child > end > end > > class OuterClass > class ConcreteInnerClass < AbstractInnerClass > end > end > > I'd love to be able to call ConcreteInnerClass#do_something and know > that it can find OuterClass. Is there a way for me to do this? Maybe like this? class AIC def my_container_class Object.const_get self.class.to_s.split(/::/)[0...-1].join("::") end end ==>nil class Out class In < AIC end end ==>nil Out::In.new.my_container_class ==>Out This will give an error if there is no outer class, but you should be able to wrap the core code line in a begin...rescue block and/or check the string to ensure non-emptyness before calling Object.const_get with it. HTH, Mark