Difference between "and" and "&&"

Hi sir Gavin Sinclair [mailto:gsinclair@soyabean.com.au]

You wrote:

[On ‘and’ and ‘&&’]
I am surprise and I’m new to Ruby :slight_smile:

I do find both structurally same, but why the difference in
precedence?

Pls enlighten…

Not so long ago, Matz stated that the difference in
precedence was because natural language doesn’t observe a
high precedence for ‘and’. I thought that was a very nice
observation from a computer geek.

but the precedence of the assignment operator has always been low (my
experience only) in other languages.

Eg, x = a and (b or c) and not c

would mean putting/assigning all results (ultimately) to x…

Am I confusing “=” assignment with “=” comparison then?? :slight_smile:

Anyway, if you get any more authoriatitive answers (I may be
confusing the issue with something else :), this one should be a FAQ.

Many thanks.

Cheers,
Gavin

Kind regards,
-botp

Hi sir Gavin Sinclair [mailto:gsinclair@soyabean.com.au]

You wrote:

[On ‘and’ and ‘&&’]
I am surprise and I’m new to Ruby :slight_smile:

I do find both structurally same, but why the difference in
precedence?

Pls enlighten…

Not so long ago, Matz stated that the difference in
precedence was because natural language doesn’t observe a
high precedence for ‘and’. I thought that was a very nice
observation from a computer geek.

but the precedence of the assignment operator has always been low (my
experience only) in other languages.

Eg, x = a and (b or c) and not c

would mean putting/assigning all results (ultimately) to x…

Am I confusing “=” assignment with “=” comparison then?? :slight_smile:

No, but other languages don’t typically have ‘and’ and ‘&&’. So yes, ‘=’ has a
low precedence, but ‘and’ is lower. To achieve your example you use ‘&&’. If
Ruby had no ‘and’ it wouldn’t be confusing. Likewise if ‘and’ was just sugar
for ‘&&’. AFAIK, no other langugage is like Ruby in this regard, so it’s bound
to be confusing at least once. I believe Matz has designed it correctly,
though. ‘and’ and ‘&&’ are subtly different and can be used to communicate
different programmer intent:

result = value && value.size
param = param || default_value
action() and consequent_action()
action() or alternative_action()

Without thinking about precedence, the code above just reads nicely: the
subtleties of the intended high-level intention are communicated very well
through the symbols used.

Gavin

···

From: “Peña, Botp” <botp@delmonte-phil.com

It also works very well if you do:

param = param || default_value and handle(param)

This way, handle(param) is only called if the result of param ||
default_value is true.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.11.21 at 09.51.47

···

On Thu, 21 Nov 2002 20:45:30 +0900, Gavin Sinclair wrote:

result = value && value.size
param = param || default_value
action() and consequent_action()
action() or alternative_action()

Without thinking about precedence, the code above just reads
nicely: the subtleties of the intended high-level intention are
communicated very well through the symbols used.