From: Robert Klemme Date: 2009-04-04T03:34:35+09:00 Subject: Re: Class in a Class problem On 03.04.2009 20:05, matt neuburg wrote: > Robert Klemme wrote: > >>> class A >>> def b >>> Class.new do >>> def initialize(a) >>> @a = a >>> end >>> attr :a >>> end.new(self) >>> end >>> # other stuff that an A knows how to do >>> end >> You are creating new classes all the time. I don't think this is a good >> idea. > > I'm not sure what you mean about new *classes*. There is a Class.new in your definition of method b which means, every time the method is invoked a new anonymous class is created - regardless whether b is invoked several times on one or multiple instances. > It is certainly true > that I didn't do anything prevent the user from calling "b" twice on the > same A instance, thus getting two different objects pointing at that > instance: > > a = A.new > b = a.b # b is an instance whose "@a" points at "a" > bb = a.b # bb is a different instance whose "@a" points at "a" > > But that's easy to solve if desired. Obviously the real problem here is > that I couldn't fathom what the OP was actually trying to *do*. m. I had similar difficulties. That's why I asked. Kind regards robert