Alistair
Alistair

Reputation: 8706

Why does Ruby have both the && and 'and' operators?

I understand the difference between them, but I can't work out why they're both included in the language. Surely having both just causes confusion?

Upvotes: 1

Views: 160

Answers (4)

Mladen Jablanović
Mladen Jablanović

Reputation: 44080

  • Perl has the same doublets, even with the same precedence difference as in Ruby.
  • Ruby was strongly influenced by Perl.

I believe one should look no further.

Upvotes: 2

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230336

Their precedence is different, so they are not equivalent.

My rule of thumb is as follows: use && for logical expressions and use and for control flow.

Examples

# logical expressions
if user.first_name == 'Bob' && user.last_name == 'Jones'

# control flow
worker.do_this and worker.and_also_do_this_if_that_went_well

Upvotes: 5

Mark Bolusmjak
Mark Bolusmjak

Reputation: 24399

This is the best explanation I've seen:

http://avdi.org/devblog/2010/08/02/using-and-and-or-in-ruby/

Upvotes: 0

Related Questions