module MJ; class Visitable def accept( visitor ) method_name = nil klass = self.class.ancestors.find do |ancestor| method_name = "visit_#{ancestor.name.split('::')[-1]}" visitor.respond_to?( method_name ) end if klass return visitor.send( method_name, self ) else raise "#{visitor.class}: Should not happen!" end end end end