gwtmp01 / mac.com wrote: > > On Jun 21, 2006, at 12:51 PM, Robert Dober wrote: ... >> One can create an accessor to @b at any time >> >> class A >> attr_accessor :bclass >> end >> A.new.bclass.new #voil>> > > > Yeah, I shouldn't have written it as creating a new class for each > instance of A. Something like: > > class A > @bclass = Class.new { > # anonymous class def here > } > # details left for the reader... > end > > would be better. Yes, you can always reopen A to create an accessor to > @bclass but that is a general characteristic of any Ruby code. I think > tucking the reference to the class in an instance variable hides it a > 'little' better than having the reference be a constant in A. A::B.new > is pretty accessible as is changing the visibility of B.new. You can hide it a bit better using a closure, and then attr_accessor won't expose it. (Sorry if someone suggested this already...I haven't been following closely.) class A class << self bclass = Class.new do def foo; puts "foo in bclass"; end end define_method :new do |*args| bclass.new(*args) end end end A.new.foo # ==> foo in bclass -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407