From: "Pe?a, Botp" <botp / delmonte-phil.com


> Hi sir Gavin Sinclair [mailto:gsinclair / soyabean.com.au]
>
> You wrote:
> >
> > > [On 'and' and '&&']
> > > I am surprise and I'm new to Ruby :-)
> > >
> > > 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?? :-)


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