Henry Gilbert wrote: > Dear Ruby Community, > > One day I hope to learn your trade :) > > But before that ... > A reasonably arrogant guy came out blasting in a web-forum about how OO is > crap and all that. > > He offered a challenge which I tried to defeat but in VB.NET > Now VB.NET sucks simply because the Visual Studio IDE that comes with it > is all very boring, slow, bureacratic, clunky and when you get a spark of > inspiration it hangs saying "Please wait while we update your Help File" > (for 20 minutes) > > Pure Joy-Killer > > His challenge is as follows: > > * I wont give the URL yet because I want you guys to come with the best > solution, don't worry I wont take credit for it - will post the website > and say this is the RUBY's solution loggin in as Anonymous * > > [QUOTE] > > Whole OO is step back in programming. Encapsulation is trivial > and unnecessary, inheritance wrong and polymorphism weak .. > > OK, here is my favorite 'Tax Payer' challenge for OO languages. There are lot of different groups of people with different rules for tax calculation. One man can be in the same time member of many groups. His membership can change during program execution time. If he is member of many groups in the time tax is calculated, his tax is the greatest one on the base of all groups he is member (they are clever!). Natural situation, isn't it? Now, (1) define separate functions that calculate tax for each groups, and (2) write polymorphic function that calculate tax for tax payer, no matter of his membership to one or many groups in the same time. Here is code in procedural language: record tax_payer(salary,member_of) For such a small variation, OO has little to offer. Morphing is usually for when a class can be extended, especially when a series of extensions can reduce code. Simply switching the tax rate between different professions is so simple, an OO solution is likely to ADD code, not reduce it. However, that said, I think Ruby has a lot to offer to make this succinct. My code is below. It's not meant to be short, it's meant to be understandable and easily extensible. rates = { "soldier" => Proc.new{|salary| salary/10}, "professor" => Proc.new{|salary| (salary/15)+100} } taxpayers = [ {"salary" => 3000, "jobs" => ["soldier"]}, {"salary" => 5000, "jobs" => ["professor"]}, {"salary" => 6000, "jobs" => ["professor", "soldier"]} ] taxpayers.each do | taxpayer | tax = 0 taxpayer["jobs"].each do | job | rate = rates[job] raise "no such job in rate schedule: '#{job}'" if (not rate) rate_tax = rate.call(taxpayer["salary"]) tax = rate_tax if (rate_tax > tax) end print(tax) end Sean O'Dell