From: Charles Comstock Date: 2004-05-31T07:13:39+09:00 Subject: Re: Visitor Pattern in Ruby On Sat, 29 May 2004, Christoph wrote: > Charles Comstock wrote: > > >module Visitable > > def accept(visitor,&block) > > for klass in self.class.ancestors do > > break if (v = visitor.methods.include?("visit_#{klass}")) > > end > > if v > > visitor.__send__("visit_#{klass}",self,&block) > > else > > visitor.default_visit(self,&block) > > end > > end > > > > > The following "included" redefinition is superfluous since any > child of a "Visitable" class is already "Visitable" - for example > > --- > module Visitable > def accept(visitor,&block) > for klass in self.class.ancestors do > break if (v = visitor.methods.include?("visit_#{klass}")) > end > if v > visitor.__send__("visit_#{klass}",self,&block) > else > visitor.default_visit(self,&block) > end > end > end > > class A > include Visitable > end > > class B < A > end > > p A < Visitable # true > p B < Visitable # true > --- > > > def Visitable.included(kls) > > kls.module_eval <<-"end_eval" > > def self.inherited(child) > > child.module_eval "include Visitable" > > end > > end_eval > > end > >end > > > > > > > /Christoph > > > Your probably correct, originally the way I had to do it required a module eval for the whole thing and so I needed that to force it to propegate down the inheritance chain. Charlie