[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Making == symmetric?

Zach Dennis

10/1/2003 7:50:00 PM


Ok with the [1,2] == [1,2]. Point taken, I'm thinking on this before
i say something stupid.

But on the Polar Points and the Cartesian Points, wouldn't
you want them to inherit from the Point class and allow the
Point class to handle conversions, translations of it's
instantiated objects? Why would you want == to handle the
translation/conversion from Polar to Cartesian. Shouldn't the
class Point handle that itself outside of equality comparion.

Why have 2 Point classes? Why not just have 1 that has 2 methods:

convertToCartesian
convertToPolar

and then check for equality.

Zach




From: Joel VanderWerf [mailto:vjoel@PATH.Berkeley.EDU]

Zach Dennis wrote:
....
> on #1. I agree with Mark and I'd also like to say...why would you want
> to use the == to return true when comparing different objects, even if
> they are of the same class, with the same attributes. Unless one's a
pointer
> or reference and you are using an equivalency of a === test. I don't see
why
> someone would want this expected behavior from == itself.

Well, for example:

irb(main):001:0> [1,2] == [1,2]
=> true

Also, strings.

Of course, this isn't really a "class with attributes", but even for
user defined classes, I sometimes define #== to work this way. This
seems like the right thing to do for geometrical objects, like points. YMMV.

>
> on #2. I think 2 different classes should always return false when
compared
> to
> one another. Otherwise you are getting away from the simplicity and
purpose
> of a comparison in it's most generic form and you are bringing
> in special cases where "behavior is varied". If you want to allow for
> special case scenario's then perhaps make a new comparison operator/method
> and leave == alone.

irb(main):002:0> 1.0 == 1
=> true

Or, if you had two Point classes, one maintaining its state in polar
coordinates, and the other in cartesian coordinates, you might want
equality to transcend class boundaries.




1 Answer

Joel VanderWerf

10/1/2003 8:13:00 PM

0

Zach Dennis wrote:
> But on the Polar Points and the Cartesian Points, wouldn't
> you want them to inherit from the Point class and allow the
> Point class to handle conversions, translations of it's
> instantiated objects? Why would you want == to handle the
> translation/conversion from Polar to Cartesian. Shouldn't the
> class Point handle that itself outside of equality comparion.
>
> Why have 2 Point classes? Why not just have 1 that has 2 methods:
>
> convertToCartesian
> convertToPolar
>
> and then check for equality.

You're probably right. One class with implementation hidden from the
user is better. Even if you want to specify to #initialize that,
internally, a certain coordinate system would be optimal for the kinds
of operations you are about to do with this point yadda yadda, that
doesn't really justify having two classes.

I was trying to come up with an analogue of 1 == 1.0 for some other
kinds of entities besides numbers. Maybe if you subclass String in a
semantically uninteresting way:

class MyString < String
def <<(s)
raise if s == "bad data"
super
end
end

my_string = MyString.new

my_string << "x" << "y"

if my_string == "xy"
puts "found xy"
end

In this case, == already does the right thing, though.