[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Directionality of comparasion operators

Tobias Weber

5/27/2008 11:32:00 AM

Hi,
maths says that the value of expressions like x == y and y == x is
always equal. And indeed most programmers wouldn't think twice about
using x < y instead of y > x (except maybe those versed in machine
language...).

But direction does matter in ruby, where operators are just messages
sent to the left operand. This often bites me when using ===.

Have you had bugs fixable by rotating an expression?

--
Tobias Weber
10 Answers

Rick DeNatale

5/27/2008 12:13:00 PM

0

On Tue, May 27, 2008 at 7:34 AM, Tobias Weber <towb@gmx.net> wrote:
> Hi,
> maths says that the value of expressions like x == y and y == x is
> always equal. And indeed most programmers wouldn't think twice about
> using x < y instead of y > x (except maybe those versed in machine
> language...).
>
> But direction does matter in ruby, where operators are just messages
> sent to the left operand. This often bites me when using ===.

=== is not, and is not intended to be the same as ==, nor is it
intended to be symmetrical.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Mark Wilden

5/27/2008 2:18:00 PM

0

On May 27, 2008, at 4:34 AM, Tobias Weber wrote:

> But direction does matter in ruby, where operators are just messages
> sent to the left operand. This often bites me when using ===.
>
> Have you had bugs fixable by rotating an expression?

There are other operators that aren't commutative (like /), but it
does take extra thought to remember that === is among them. That bit
me in a recent post here.

///ark

Todd Benson

5/27/2008 2:36:00 PM

0

On Tue, May 27, 2008 at 6:34 AM, Tobias Weber <towb@gmx.net> wrote:
> Hi,
> maths says that the value of expressions like x == y and y == x is
> always equal. And indeed most programmers wouldn't think twice about
> using x < y instead of y > x (except maybe those versed in machine
> language...).
> But direction does matter in ruby, where operators are just messages
> sent to the left operand. This often bites me when using ===.
>
> Have you had bugs fixable by rotating an expression?

Operators depend on context (think of the use of <). === is no
different. There are, after all, a limit to the symbols you want to
remember, right :-)

A number of symbols cannot be overridden during the interpretation of
a script/program, such as &&, ternary operator, assignment operators,
and several others.

When, I first saw ===, I immediately hit the docs out of curiosity,
having never seen it before, and therefore never had a problem with
it.

Todd

Mark Wilden

5/27/2008 2:48:00 PM

0

On May 27, 2008, at 7:36 AM, Todd Benson wrote:

> When, I first saw ===, I immediately hit the docs out of curiosity,
> having never seen it before, and therefore never had a problem with
> it.

I like the moniker David Black cites: "the threequal operator."

///ark

Rick DeNatale

5/27/2008 2:57:00 PM

0

On Tue, May 27, 2008 at 10:48 AM, Mark Wilden <mark@mwilden.com> wrote:
> On May 27, 2008, at 7:36 AM, Todd Benson wrote:
>
>> When, I first saw ===, I immediately hit the docs out of curiosity,
>> having never seen it before, and therefore never had a problem with
>> it.
>
> I like the moniker David Black cites: "the threequal operator."

It's cute, but I think it gives the wrong impression that === is
another form of ==, it's really not, since it's prime motivation is
use with the case statement, I think it's better to think of it has
having a meaning more like a general sense of 'matches'.


--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Mark Wilden

5/27/2008 3:07:00 PM

0


On May 27, 2008, at 7:57 AM, Rick DeNatale wrote:

> ['threequal' is] cute, but I think it gives the wrong impression
> that === is
> another form of ==, it's really not, since it's prime motivation is
> use with the case statement, I think it's better to think of it has
> having a meaning more like a general sense of 'matches'.

Since case statements are traditionally about comparing values (in
Cxx), I can buy that === is an extension of that. In any case, what's
a better name? "The case matching operator" doesn't exactly trip off
the tongue. :)

But your point is taken.

///ark

Tobias Weber

5/27/2008 3:15:00 PM

0

In article <481BDC1C-C183-43C0-80B0-CA2B5C50FFD2@mwilden.com>,
Mark Wilden <mark@mwilden.com> wrote:

> Cxx), I can buy that === is an extension of that. In any case, what's
> a better name? "The case matching operator" doesn't exactly trip off

= assignement
== equality
=== likeness?

--
Tobias Weber

Rick DeNatale

5/27/2008 3:29:00 PM

0

On Tue, May 27, 2008 at 11:14 AM, Tobias Weber <towb@gmx.net> wrote:
> In article <481BDC1C-C183-43C0-80B0-CA2B5C50FFD2@mwilden.com>,
> Mark Wilden <mark@mwilden.com> wrote:
>
>> Cxx), I can buy that === is an extension of that. In any case, what's
>> a better name? "The case matching operator" doesn't exactly trip off
>
> = assignement
> == equality
> === likeness?

No, I don't think that

3 is like the range (0..5)
or
1.2 is like the class object Float

or
"abc" is like the regexp /.../ or /[abc]{3}/ or ...

perhaps === means something more like "recognizes as one of its own."

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

Mark Wilden

5/27/2008 3:31:00 PM

0

On May 27, 2008, at 8:14 AM, Tobias Weber wrote:

> = assignement
> == equality
> === likeness?

Except that likeness is commutative. If one thing is like another,
then vice versa.

///ark

Tobias Weber

5/27/2008 3:37:00 PM

0

In article <C3B253EF-9A1E-45AD-9D50-185DEA3DAA60@mwilden.com>,
Mark Wilden <mark@mwilden.com> wrote:

> > === likeness?
>
> Except that likeness is commutative. If one thing is like another,

Right. The match operator =~ clearly looks directional.
membership?

--
Tobias Weber