From: Robert Klemme Date: 2003-09-12T23:17:11+09:00 Subject: Re: OO Challenge "Robert Feldt" schrieb im Newsbeitrag news:oprvd068apoglyup@mail1.telia.com... [snip] I prefer this approach since it is more OO in the sense that rules and their names are better bound together. robert require 'set' # abstract base, not really needed class TaxGroup class << self attr_reader :all_groups def inherited(gr) $stderr.puts "Group #{gr}" ( @all_groups ||= Set.new ) << gr end end end # tax groups class TaxGroupSoldier < TaxGroup def calc_tax(emp) emp.salary/10.0 end end class TaxGroupProf < TaxGroup def calc_tax(emp) emp.salary/15.0+100 end end # tax payers class TaxPayer attr_reader :tax_groups attr_accessor :salary def initialize(sal, groups = []) @salary = sal @tax_groups = Set.new @tax_groups.merge groups end def tax() self.tax_groups.map do |gr| gr.new.calc_tax(self) end.max end end # example taxPayers = [ TaxPayer.new(3000,[TaxGroupSoldier]), TaxPayer.new(5000,[TaxGroupProf]), TaxPayer.new(6000,[TaxGroupProf, TaxGroupSoldier]), ] taxPayers.each do |p| puts "#{p.inspect}: #{p.tax}" end