[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: OO Challenge

Weirich, James

9/12/2003 2:57:00 PM

I'll throw in my version. Logically it is structured very much like Robert
Klemme's except that I made lighter weight choices for some of the
abstractions. For example, I use simple procs for the tax groups and my
TaxPayer is Struct based. What is really interesting is that despite the
differences in representation, the last bits of our programs are identical
except for minor spelling differences.

Here's the code.

-----------------------------------------------------
Soldier = proc { |p| p.salary / 10.0 }
Professor = proc { |p| p.salary / 15.0 + 100 }

TaxPayer = Struct.new(:salary, :tax_classes)
class TaxPayer
def tax
tax_classes.collect { |tc| tc.call(self) }.max
end
end

taxpayers = [
TaxPayer.new(3000, [Soldier])
TaxPayer.new(5000, [Professor])
TaxPayer.new(6000, [Professor, Soldier])
]

Taxpayers.each { |tp| printf "%0.2f\n", tp.tax }
----------------------------------------------------

Disclaimer: I don't believe this example says anything deep or significant
about OO verse non-OO.

--
-- Jim Weirich / Compuware
-- FWP Capture Services
-- Phone: 859-386-8855

4 Answers

Robert Klemme

9/12/2003 3:44:00 PM

0


"Weirich, James" <James.Weirich@FMR.COM> schrieb im Newsbeitrag
news:1C8557C418C561429998C1F8FBB283A728BA48@MSGDALCLB2WIN.DMN1.FMR.COM...
> I''ll throw in my version. Logically it is structured very much like
Robert
> Klemme''s except that I made lighter weight choices for some of the
> abstractions. For example, I use simple procs for the tax groups and my
> TaxPayer is Struct based. What is really interesting is that despite
the
> differences in representation, the last bits of our programs are
identical
> except for minor spelling differences.

Probably there''s not too much room for variations with this small example.
Or we think along the same lines. Could be interesting to see with what
others come up.

Rereading the original posting I think the author of these lines has not
yet fully understand OO:

"Tax Payer problem is all about object (tax_payer) - class (soldier ...)
membership and polimorphic functions, just it is not that simple as
designers of OO languages believe (object is member of only one class (and
superclasses) and does not change its membership, function version can be
decided solely on class membership) - hence one is forced to handle
membership relation on his own, and OO support integrated in the language
is shown to be restrictive and cluttering."

In fact there are only few languages in which an instance can change its
class at runtime. I''m not sure about belonging to several classes at the
same time. Even if there were, I''d be curios how that should be modelled
(after all you have to invoce *all* calculation methods before deciding on
the return value.)

But that doesn''t necessarily mean that OO languages are not well suited
for this problem. In fact, any of the ruby solutions looks cleaner to me
than the VB examples. My 2 cent...

> Here''s the code.
[snip]

Great short implementation! I always like to see how small programs can
get in Ruby. Unfortunately that reminds me that I sometimes do seem to
make things unnecessary complicated... *sigh*

OTOH we don''t know what these groups are supposed to do other than
calculating the tax value. They sure have some weird code that uniquely
identifies... :-)

> Disclaimer: I don''t believe this example says anything deep or
> significant about OO verse non-OO.

Very much indeed so. I still wonder why the OP did not put the original
link here - if there is any at all...

Regards

robert

Richard Dale

9/12/2003 4:42:00 PM

0

Weirich, James wrote:

> I''ll throw in my version. Logically it is structured very much like
> Robert Klemme''s except that I made lighter weight choices for some of the
> abstractions. For example, I use simple procs for the tax groups and my
> TaxPayer is Struct based. What is really interesting is that despite the
> differences in representation, the last bits of our programs are identical
> except for minor spelling differences.
>
> Here''s the code.
> def tax
> tax_classes.collect { |tc| tc.call(self) }.max
> end
I like James'' solution best because he has tax_classes ''collect''ing - which
seems to model the real world well :).

-- Richard

Henry Gilbert

9/12/2003 10:04:00 PM

0

Link is above there somewhere.
http://tools.devchannel.org/comments.pl?sid=3343&op=&threshold=0&commentsort=0&mode=thread&tid=39&p...

(great it was still in my clipboard!)

The article was about Eiffel.
But the guy blasted OO ..
I know from experience that the least you know - the more you think you
know :P
So obviously that guy was whizzing on an ego trip.

He fails to realize that OO is to encapsulate and reduce complexity for
larger programs - not trivial examples.

His example is silly.
As OO is *usually* built on procedural-like code.
Now .. if he were to have an example involving a more complex application
Then he would lose ground - he is cheating, setting the rules - with an example of
20 lines or so.

As I stated I tried VB.NET because you can do *illegal* things
that makes code more sugary-like.

With C# you can make classes morph into others - while introducing some
ugly parameters. (Perhaps) the solution could have been more concise.

But I was aiming for the solution to be readable when declaring.
That is IMO .. what OO is for. Work hard to make it easier.

I was close to finding a solution - but no EVAL in VB, and didn''t want to
thread at MSDN for hours, .. I got sick of Visual Studio .NET slow,
crawling once more -hanging on "Wait 20 minutes ..." (P-OFF MS! grrrr).

Refreshing to be here under Linux again :)

Thanks again.

I will look into RUBY more
I am looking for a nice language.
I tried Smalltalk but couldnt get round it. I feel even embarrased about
it :(
Ruby seems to be more my style!

I deffy want to re-write a site which I began with C#.
So any advice on RUBY (ie links) for rendering pages and dealing with Response Fields
is much appreciated.

Of course I will always be searching through posts here trying to learn as much as
poss.

All the best

Henry Gilbert

Robert Klemme

9/15/2003 7:38:00 AM

0


"Henry Gilbert" <nospam@alliancetec.com> schrieb im Newsbeitrag
news:pan.2003.09.12.22.03.43.604791@alliancetec.com...
> He fails to realize that OO is to encapsulate and reduce complexity for
> larger programs - not trivial examples.

I think the posted examples show that even simple programs can benefit
from OO (and especially Ruby) - at least by increased readability but also
by reduced code (you get polymorphism and a lot of other stuff for free).

> I deffy want to re-write a site which I began with C#.
> So any advice on RUBY (ie links) for rendering pages and dealing with
Response Fields
> is much appreciated.

Look out for eruby, amrita, apache mod-ruby and the like.

> Of course I will always be searching through posts here trying to learn
as much as
> poss.

Go ahead, you''ll find plenty. :-)

Regards

robert