Directionality of comparasion operators

T

Tobias Weber

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?
 
R

Rick DeNatale

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.
 
M

Mark Wilden

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
 
T

Todd Benson

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
 
M

Mark Wilden

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
 
R

Rick DeNatale

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'.
 
M

Mark Wilden

['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
 
T

Tobias Weber

Mark Wilden said:
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?
 
R

Rick DeNatale

= 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."
 
T

Tobias Weber

=== likeness?

Except that likeness is commutative. If one thing is like another,[/QUOTE]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top