0

Possible Duplicate:
Is there any wisdom behind “and”, “or” operators in Ruby?
Ruby logical operators

Looking for an explanation of Ruby's and operator, contrasted with the && operator. I get from this post that there's a precedence difference, but the author simply recommends against using and, rather than revealing what meaningful purpose it might serve in the language.

Additionally, what's the practical reason for using one over the other?

Community
  • 1
  • 1
jefflunt
  • 33,527
  • 7
  • 88
  • 126
  • && is more important (when solving execution order) than and. And same with or. – Smar Nov 10 '11 at 21:16
  • I rephrased my question to request an additional answer: what's the practical reason for using one over the other. Like I said, I get the difference in precedence, but what I don't get is when you'd prefer one over the other. – jefflunt Nov 10 '11 at 21:26
  • I think it’s still duplicate. Just matter of taste, since it’s simply syntactic and execution order difference. – Smar Nov 10 '11 at 21:28
  • Ok, but the additional understanding and explanation I asked for wasn't provided, so that makes the closure not terribly helpful to people that are left without a complete answer in the future. Can someone go to the original question, and edit the accepted answer to touch on the "why" part of this question? – jefflunt Nov 10 '11 at 21:34

1 Answers1

0

and is ultra-low-precedence, like in Perl. Many Ruby programmers seem to advocate against and, but I think it's to be preferred over && in most cases -- but do understand the difference in precedence!

Marnen Laibow-Koser
  • 5,959
  • 1
  • 28
  • 33
  • 1
    Is there a practical reason you'd use one form instead of the other - something where `and` really does the trick that `&&` doesn't, or where `and` makes for shorter/cleaner code? – jefflunt Nov 10 '11 at 21:27
  • I like `and` for simple conditions -- `if x < 5 and y > 3`. That's what Perl's `and` was designed for (because of its ultra-low precedence). OTOH, a lot of Ruby programmers would insist on `&&` there, for reasons that escape me. I'd only use `&&` for complex boolean arithmetic myself. – Marnen Laibow-Koser Nov 10 '11 at 21:30