[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Comparable glitch/aspect?

David Dyer-Bennet

6/26/2006 7:54:00 PM

I had a class that included Comparable and defined <=>, I think
correctly, where a check for != between objects was returning false
when it should have returned true. Reading carefully, Comparable says
it defines a bunch of comparison operators, but *not* !=. Ruby
documentation says that != comes automatically from ==, though.
Anyway, I changed the class to get rid of comparable and define ==
(I'm at the playing level, but in fact inequality comparisons aren't
important for this class anyway), and things started working as I
expected.

Are there weirdnesses to how these things work that I'm missing? Or
is it most likely that I just had something simple wrong and didn't
realize it, and managed to accidentally fix it while thrashing around
(these things happen!)? Should my <=> + Comparable implementation have
provided a !=?

(I'm coming to Ruby from almost 4 decades of programming experience,
and considerable C++ and object-oriented perl and PHP work, which no
doubt biases the particular kinds of newbie mistakes I will commit.)
--
David Dyer-Bennet, <mailto:dd-b@dd-b.net>, <http://www.dd-b.net...
RKBA: <http://www.dd-b.net/...
Pics: <http://dd-b.lighthunter... <http://www.dd-b.net/dd-b/Snapshot...
Dragaera/Steven Brust: <http://dragaera...
6 Answers

Tim Hunter

6/26/2006 8:12:00 PM

0

David Dyer-Bennet wrote:
> I had a class that included Comparable and defined <=>, I think
> correctly, where a check for != between objects was returning false
> when it should have returned true. Reading carefully, Comparable says
> it defines a bunch of comparison operators, but *not* !=. Ruby
> documentation says that != comes automatically from ==, though.
> Anyway, I changed the class to get rid of comparable and define ==
> (I'm at the playing level, but in fact inequality comparisons aren't
> important for this class anyway), and things started working as I
> expected.
>
> Are there weirdnesses to how these things work that I'm missing? Or
> is it most likely that I just had something simple wrong and didn't
> realize it, and managed to accidentally fix it while thrashing around
> (these things happen!)? Should my <=> + Comparable implementation have
> provided a !=?
>
> (I'm coming to Ruby from almost 4 decades of programming experience,
> and considerable C++ and object-oriented perl and PHP work, which no
> doubt biases the particular kinds of newbie mistakes I will commit.)

I don't have the Ruby source code in front of me but I'm pretty sure
that Comparable says that == is true when <=> returns 0.

module Comparable
def ==(other)
return (self <=> other) == 0
end
end

So != is true when <=> returns either -1 or 1.

Is it possible you're expecting 0 to be false? In Ruby everything is
true except false and nil.

David Dyer-Bennet

6/26/2006 9:12:00 PM

0

Tim Hunter <sastph@sas.com> writes:

> David Dyer-Bennet wrote:
> > I had a class that included Comparable and defined <=>, I think
> > correctly, where a check for != between objects was returning false
> > when it should have returned true. Reading carefully, Comparable says
> > it defines a bunch of comparison operators, but *not* !=. Ruby
> > documentation says that != comes automatically from ==, though.
> > Anyway, I changed the class to get rid of comparable and define ==
> > (I'm at the playing level, but in fact inequality comparisons aren't
> > important for this class anyway), and things started working as I
> > expected. Are there weirdnesses to how these things work that I'm
> > missing? Or
> > is it most likely that I just had something simple wrong and didn't
> > realize it, and managed to accidentally fix it while thrashing around
> > (these things happen!)? Should my <=> + Comparable implementation have
> > provided a !=? (I'm coming to Ruby from almost 4 decades of
> > programming experience,
> > and considerable C++ and object-oriented perl and PHP work, which no
> > doubt biases the particular kinds of newbie mistakes I will commit.)
>
> I don't have the Ruby source code in front of me but I'm pretty sure
> that Comparable says that == is true when <=> returns 0.
>
> module Comparable
> def ==(other)
> return (self <=> other) == 0
> end
> end
>
> So != is true when <=> returns either -1 or 1.
>
> Is it possible you're expecting 0 to be false? In Ruby everything is
> true except false and nil.

Hmmm; I thought originally when you suggested it that that must have
been the problem, but actually I don't think so. I was thinking
wrong, but not in places where it mattered then, I don't think. My
<=> implementation returned either the value of a <=> between strings,
or else a literal 0 if no difference could be found -- but <=> is -1,
0, 1, so that's correct.

I found this while working on unit tests for the thing, so at this
point I not only have something that works, but something that will
tell me if it stops working in the future, so we'll see if it comes
back.

It's useful to be reminded about the 0 not being false thing, even if
that wasn't the cause of the specific problem.
--
David Dyer-Bennet, <mailto:dd-b@dd-b.net>, <http://www.dd-b.net...
RKBA: <http://www.dd-b.net/...
Pics: <http://dd-b.lighthunter... <http://www.dd-b.net/dd-b/Snapshot...
Dragaera/Steven Brust: <http://dragaera...

David Dyer-Bennet

6/26/2006 10:30:00 PM

0

David Dyer-Bennet <dd-b@dd-b.net> writes:

> Tim Hunter <sastph@sas.com> writes:
>
> > David Dyer-Bennet wrote:
> > > I had a class that included Comparable and defined <=>, I think
> > > correctly, where a check for != between objects was returning false
> > > when it should have returned true. Reading carefully, Comparable says
> > > it defines a bunch of comparison operators, but *not* !=. Ruby
> > > documentation says that != comes automatically from ==, though.
> > > Anyway, I changed the class to get rid of comparable and define ==
> > > (I'm at the playing level, but in fact inequality comparisons aren't
> > > important for this class anyway), and things started working as I
> > > expected. Are there weirdnesses to how these things work that I'm
> > > missing? Or
> > > is it most likely that I just had something simple wrong and didn't
> > > realize it, and managed to accidentally fix it while thrashing around
> > > (these things happen!)? Should my <=> + Comparable implementation have
> > > provided a !=? (I'm coming to Ruby from almost 4 decades of
> > > programming experience,
> > > and considerable C++ and object-oriented perl and PHP work, which no
> > > doubt biases the particular kinds of newbie mistakes I will commit.)
> >
> > I don't have the Ruby source code in front of me but I'm pretty sure
> > that Comparable says that == is true when <=> returns 0.
> >
> > module Comparable
> > def ==(other)
> > return (self <=> other) == 0
> > end
> > end
> >
> > So != is true when <=> returns either -1 or 1.
> >
> > Is it possible you're expecting 0 to be false? In Ruby everything is
> > true except false and nil.
>
> Hmmm; I thought originally when you suggested it that that must have
> been the problem, but actually I don't think so. I was thinking
> wrong, but not in places where it mattered then, I don't think. My
> <=> implementation returned either the value of a <=> between strings,
> or else a literal 0 if no difference could be found -- but <=> is -1,
> 0, 1, so that's correct.

Ah! And now I see where I was expecting an integer to be false (in
the if modifiers on the series of return statements), so that *is*
what was wrong (and the == implementation was different and didn't
encounter that problem).

Thanks again!
--
David Dyer-Bennet, <mailto:dd-b@dd-b.net>, <http://www.dd-b.net...
RKBA: <http://www.dd-b.net/...
Pics: <http://dd-b.lighthunter... <http://www.dd-b.net/dd-b/Snapshot...
Dragaera/Steven Brust: <http://dragaera...

abourick

11/16/2010 10:08:00 PM

0

Massine a ?crit :
> On 16 nov, 20:09, abourick <bourrique-a...@yahoo.br> wrote:
>> Massine a crit :
>>
>>> Dures, tr s dures, les photos de Kai Wiedenh fer
>> Avec un nom pareil il a d avoir des anc tres dans la SS.
>>
>> Tss, tss, pas s rieuse ta source, tSS, tSS...
>
> Je regrette la disparition des SS, c'est les seuls qui savent bien
> s'occuper de vous!


"Ach ! Les muSSulmans zont tes incapaples" tonne Hitler du fond des enfers.


> Peut-?tre la source n'est pas fiable ? tes yeux, mais les photos sont
> bien l? pour d?montrer que vous ?tes les dignes h?ritiers des nazis..


Ces photos avec des palechtichiens gras comme des chanoines et Gaza qui
ressemble ? une bonbonni?re sont vraiment insupportables ! :-D


http://morrigandunn.blo...
http://morrigandunn.blo...2010/08/gaza-pictures-what-media-does-not-want.html
http://www.juif.org/le-mag/344,gaza-le-camp-de-concentratio...







The Fustigator

11/17/2010 10:35:00 AM

0

On Tue, 16 Nov 2010 12:43:38 -0800 (PST), Massine <sbouzidi@gmail.com>
wrote:

>On 16 nov, 20:09, abourick <bourrique-a...@yahoo.br> wrote:
>> Massine a crit :
>>
>> > Dures, tr s dures, les photos de Kai Wiedenh fer
>>
>> Avec un nom pareil il a d avoir des anc tres dans la SS.
>>
>> Tss, tss, pas s rieuse ta source, tSS, tSS...

>Je regrette la disparition des SS, c'est les seuls qui savent bien
>s'occuper de vous!

Votre discours pue autant que celui d'abourick.
Les photos de Kai valent mieux, beaucoup mieux que ce que vous en
faites, vous travaillez pour un lobby sioniste ?

abourick

11/17/2010 11:56:00 AM

0

The Fustigator a ?crit :
> On Tue, 16 Nov 2010 12:43:38 -0800 (PST), Massine <sbouzidi@gmail.com>
> wrote:
>
>> On 16 nov, 20:09, abourick <bourrique-a...@yahoo.br> wrote:
>>> Massine a crit :
>>>
>>>> Dures, tr s dures, les photos de Kai Wiedenh fer
>>> Avec un nom pareil il a d avoir des anc tres dans la SS.
>>>
>>> Tss, tss, pas s rieuse ta source, tSS, tSS...
>
>> Je regrette la disparition des SS, c'est les seuls qui savent bien
>> s'occuper de vous!
>
> Votre discours pue autant que celui d'abourick.
> Les photos de Kai valent mieux, beaucoup mieux que ce que vous en
> faites, vous travaillez pour un lobby sioniste ?


On fait semblant de critiquer maSSine le nazislamiste mais ce n'est
qu'une feinte puisque pour Ze Defecator l'ennemi ? abattre reste le juif
abhorr?.