From: Christoph Date: 2004-05-29T00:51:40+09:00 Subject: Re: Visitor Pattern in Ruby 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