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 — the difference in Ruby that can break your code

Post cover: && vs and — the difference in Ruby that can break your code
This content has been automatically translated from Ukrainian.
In Ruby, there are two logical "and" operators - && and and. At first glance, they seem similar, but they have different precedence.
&& has high precedence, so it is usually used for logical expressions, conditions, and loops.
and has low precedence, so it is often used for control flow when multiple actions need to be executed sequentially.
Example with &&:
result = true && false
# first evaluates true && false => false
# result => false
Example with and:
result = true and false
# Ruby reads as (result = true) and false
# first assigns result = true
# then evaluates logical and with false, but this does not change the variable result
result # => true
When and what to use?
  • Use && for logic and conditions.
  • Use and for sequential execution of actions (control flow), for example return true and log_success. But it's better not to do this. Because it can be a bit confusing.
Precedence determines the order of evaluation, 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
Oct 29, '25 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 they check and when to use them
Oct 29, '25 20:47

==, equal?, eql?, === in Ruby: what they check and when to use them

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

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

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

module_function in Ruby: when module methods are available as module methods and as functions

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

What is memoization in Ruby?

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

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

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Variables in Ruby: @, @@ and class instance variable
Oct 30, '25 20:54

Variables in Ruby: @, @@ and class instance variable

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

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

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

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

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

What is the Vanilla Rails approach?

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

What is Elasticsearch and how does it work?

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

What is a time-series database?

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