From: Jacob Fugal Date: 2006-06-21T07:19:53+09:00 Subject: Re: Achieve pure object oriented design in Ruby On 6/20/06, Robert Dober wrote: > The real problem is requirement 2) of the OP, instanciation of class B > shall only be possible in class A, e.g. in an instance of A, I do not think > this is possible, but I would not bet on it (not much). > It would already be nice to have some code which defends against the vanilla > instanciation of B outside of an A instance. > I did not yet come up with such a solution though. > Any ideas? One way would be to make B.new protected/private (similar to the way it's done in the Singleton mixin). But once you've done that, I'm not sure if there's a clean way to allow A to use that method -- Ruby doesn't have "friends" ala C++ (and I'm glad for it). Of course, once it's protected you *could* have A use send explicitly: class B class << self protected :new end end class A def create_b @b_objs << B.send(:new) end end a = A.new a.create_b # works fine B.new # raises NoMethodError This works, but it doesn't feel very clean :/ Jacob Fugal