From: Robert Feldt Date: 2003-09-12T22:02:36+09:00 Subject: Re: OO Challenge Henry Gilbert skrev den Fri, 12 Sep 2003 21:37:04 +0900: > record tax_payer(salary,member_of) > record tax_rule_data(multiplier, additional) > global tax_rule > > procedure tax(payer) > � � res:=0 > � � every r:=tax_rule[! payer.member_of ] do > � � � � � � � if res less_than t:=payer.salary/r[1]+r[2] > � � � � � � � � � � � � � � � � � � � � then res:=t > � � return res > end > > procedure main() > � � tax_rule:=table() > � � tax_rule["soldier"]:=tax_rule_data(10,0) > � � tax_rule["professor"]:=tax_rule_data(15,100) > � � A:=tax_payer(1000,["soldier","professor"]) > � � write(tax(A)) > end > Without any refactoring or rethinking of his "problem" here is a Ruby version: Rule = Struct.new("Rule", :multiplier, :additional) class Rule def calc_tax(salary) salary/(multiplier || 1) + (additional || 0) end end Rules = {"soldier" => Rule.new(10.0, 0), "professor" => Rule.new(15.0, 100)} TaxPayer = Struct.new("TaxPayer", :salary, :member_of) class TaxPayer def tax Rules.select {|n,r| member_of.include?(n)}.map do |profession, rule| rule.calc_tax(salary) end.max end end a = TaxPayer.new(1000, ["soldier", "professor"]) p a.tax # => 166.666666667 Not the way I would structure this but its close to his code. Which one is more "elegant" is subjective IMHO but I don't think he has got a strong case... ;) It also seems like a lousy example for comparisons between languages and paradigms. Then again most such comparisons are a lousy idea... ;) Regards, Robert