On Thu, 23 Nov 2006 Ara.T.Howard / noaa.gov wrote:

> to clean it up pull 'init_attr' into it's own module and extend only those
> classes that need this functionality with the module.

since my first method was slightly flawed, here's the above plus a fix:


   harp:~ > cat a.rb
   module InitAttr
     def init_attr a, &init
       ivar = "@#{ a }"
       define_method a do
         this = class << self; self; end
         begin
           instance_variable_set ivar, instance_eval(&init)
         ensure
           this.module_eval{ define_method(a){ instance_variable_get ivar } }
         end
       end
     end
   end

   class C
     extend InitAttr
     init_attr(:list){ Array.new }
     def push(val) list.push val end
   end

   obj = C.new
   p obj.list
   obj.push 42
   p obj.list
   p C.new.list


   harp:~ > ruby a.rb
   []
   [42]
   []


-a
-- 
my religion is very simple.  my religion is kindness. -- the dalai lama