Jacob Fugal wrote:
>  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

We can just keep playing the closure game:

  class A
    def initialize
      class << self
        b_objs = []

        bclass = Class.new do
          def initialize
            puts "creating an instance of bclass"
          end
        end

        define_method :gimme_another_b do |*args|
          b = bclass.new(*args)
          b_objs << b
          b # return b instead of b_objs
        end

        define_method :secret_method_to_get_b_objs do
          b_objs
        end
      end
    end
  end


  a = A.new
  p a.secret_method_to_get_b_objs
  p a.gimme_another_b
  p a.secret_method_to_get_b_objs

__END__

output:

[]
creating an instance of bclass
#<#<Class:0xb7dc4aa0>:0xb7dc453c>
[#<#<Class:0xb7dc4aa0>:0xb7dc453c>]


-- 
      vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407