All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

&& vs and — difference in Ruby that can break your code

Post cover: && vs and — difference in Ruby that can break your code
This content has been automatically translated from Ukrainian.
Ruby has two logical "and" operators - && and and. At first glance, they are similar, but have different execution priority.
&& it has high priority, so it is usually used for logical expressions, conditions and loops.
and has low priority, so it is often used for control flow when several actions need to be performed sequentially.
Example with &&:
result = true && false
# first computes true && false => false
# result => false
Example with and:
result = true and false
# Ruby reads as (result = true) and false
# initially assign result = true
# then runs logical and false, but this does not change the result variable
result # => true
When and what to use?
  • Use && for logic and conditions.
  • Use and to perform actions (control flow) sequentially, for example return true and log_success. But it is better not to do this either. Because you can get a little confused.
Priority determines the order of the calculation, not the result of the logical operation itself.

This post doesn't have any additions from the author yet.

Singleton class (eigenclass) in Ruby: what it is and why it is needed
29 Oct 18:29

Singleton class (eigenclass) in Ruby: what it is and why it is needed

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
==, equal?, eql?, === in Ruby: what is checked and when to use
29 Oct 20:47

==, equal?, eql?, === in Ruby: what is checked and when to use

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Include, Extend, Prepend in Ruby: how they work and what's the difference
29 Oct 21:20

Include, Extend, Prepend in Ruby: how they work and what's the difference

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
module_function in Ruby: When module methods are available as modular and as functions
29 Oct 21:53

module_function in Ruby: When module methods are available as modular and as functions

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is memoization in Ruby?
30 Oct 10:17

What is memoization in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
is_a?, kind_of?, instance_of? — how does Ruby check object type?
30 Oct 19:55

is_a?, kind_of?, instance_of? — how does Ruby check object type?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The variables in Ruby are @, @@ and class instance variable
30 Oct 20:54

The variables in Ruby are @, @@ and class instance variable

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The difference between blank?, present?, empty? but nil? in Ruby
30 Oct 21:06

The difference between blank?, present?, empty? but nil? in Ruby

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What Middleware is in Ruby on Rails and when it is used
04 Nov 10:39

What Middleware is in Ruby on Rails and when it is used

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Vanilla Rails approach?
14 Nov 16:48

What is Vanilla Rails approach?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Elasticsearch and how does it work?
22 Nov 12:35

What is Elasticsearch and how does it work?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a time-series database?
22 Nov 12:42

What is a time-series database?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska