From: Jacob Fugal Date: 2006-06-22T07:18:05+09:00 Subject: Re: Achieve pure object oriented design in Ruby On 6/21/06, Joel VanderWerf wrote: > 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 I think you rather meant: class A def initialize @b_objs = [] end bclass = Class.new do def initialize puts "creating an instance of bclass" end end define_method :gimme_another_b do |*args| @b_objs << bclass.new(*args) end end a = A.new a.gimme_another_b # ==> creating an instance of bclass It looks to me that as long as there is no access to @b_objs, this successfully hides bclass. But as soon as @b_objs is accessible: class A attr_reader :b_objs end bclass = a.b_objs.first.class bclass.new # ==> creating an instance of bclass Not meant to be nitpicking you specifically Joel, just continuing to demonstrate that one *really* can't prevent things like this in a language with open and first-class Classes. :) Jacob Fugal