On Nov 26, 2007, at 2:20 AM, Chris Richards wrote:

> I have created a module as follows which adds an initialize method to
> the class it is included in.  The problem is that the initializer  
> method
> could be easily overriden by the includer Class.  Is there a way to
> ensure that the initialize method is run even though it may have been
> overriden??
>
> module SomeModule
>
>  def self.included(base)
>     base.class_eval do
>
>       attr_accessor :fields, :x_position, :y_position
>
>       def initialize(x_position=0, y_position=0)
>         @fields=self.class.xfields.clone
>         self.y_position=y_position
>         self.x_position=x_position
>         for field in @fields
>               eval "self."+field[:var] + " = field[:value]"
>         end
>         @fields = @fields.sort_by{|f| f[:order]} if @fields.find{|f|
> f[:order]}
>       end
>     end
>   end
>
> end
>
> Any help is appreciated.
> Thanks, Chris
> -- 
> Posted via http://www.ruby-forum.com/.
>

i'd be inclined to do something like this:

cfp:~ > cat a.rb
module M
   def M.initialize obj
     obj.instance_eval{
       @a = 42
     }
   end

   module ClassMethods
     def new *a, &b
       obj = super
     ensure
       M.initialize obj
     end
   end

   module InstanceMethods
     attr :a
   end

   def self.included other
     other.send :extend, ClassMethods
     other.send :include, InstanceMethods
   end
end

class C
   include M
end

p C.new.a


cfp:~ > ruby a.rb
42


allowing you to extend the includee's class, instances, and also keep  
your private initialize private rather than dropping it into the  
includee

regards
a @ http://codeforpeople.com/
--
share your knowledge.  it's a way to achieve immortality.
h.h. the 14th dalai lama