From: Trans Date: 2007-11-27T08:52:19+09:00 Subject: Re: Metaprogramming help On Nov 26, 4: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 This is more of an aside then an answer to your question. But it is important to note that you should not do the above. You are "falsifying" inclusion and actually injecting code. The reason I say "falsify" is because you end up with a module in the hierarchy that contains no code. If you want to inject code, then simply define a class method that does it. Don't use the included callback. BTW: To ensure a routine always runs on initialization despite #initialize, one way: class X class << self def new(*a, &b) o = allocate o.do_your_thing o.initialize(*a,&b) o end end end The other is AOP which is a much larger can of worms. T.