Monday, December 22, 2025

Operator Priority · Ponderings of an Andy


Introduction

In certainly one of my earlier roles, the crew utilized Ruby. I used to be not a person contributor at that time in my profession, so my publicity to it was lower than a lot of my crew. Nonetheless, throughout my tenure, I observed a number of “Gotchas” of the Ruby language. Every language has these in their very own means, however on this put up I will cowl one particular one.

What is the distinction between and and &&? What in regards to the distinction between or and ||?

The Gotcha

At first cross, these two blocks of code seem to do the identical factor. Let’s have a look.

irb(primary):003:0> true && false
=> false
irb(primary):004:0> true and false
=> false

By the foundations of boolean logic, each and and && statements return false.

irb(primary):005:0> true || false
=> true
irb(primary):006:0> true or false
=> true

Boolean logic holds true for the or and || statements and return true.

However, what occurs if we assign the results of this to a variable?

irb(primary):007:0> outcome = true and false
=> false
irb(primary):008:0> result1 = true && false
=> false
irb(primary):009:0> outcome
=> true
irb(primary):010:0> result1
=> false

Now we see the issue. With the and operator, outcome = true. With the && operator, result1 = false.

Trying on the or/|| operators rapidly,

irb(primary):011:0> result2 = true or false
=> true
irb(primary):012:0> result3 = true || false
=> true
irb(primary):013:0> result2
=> true
irb(primary):014:0> result3
=> true

This all appears to be like cheap. However, let’s swap the order of our true/false within the assertion.

irb(primary):015:0> result2 = false or true
=> true
irb(primary):016:0> result3 = false || true
=> true
irb(primary):017:0> result2
=> false
irb(primary):018:0> result3
=> true

Right here we see the variations once more, with the or operator setting result2 = false and the || operator setting result3 = true

What’s going on?

and and && / or and || are the identical factor, however they’ve totally different orders of priority. The (not properly formatted in my view) documentation reveals the priority desk for Ruby.

The important thing issues to level out right here:

  • && and || are above and and or, which means they are going to be evaluated first.
  • = can be above and and or (however under && and ||)

Let’s take a look at one of many examples once more:

irb(primary):015:0> result2 = false or true
=> true
irb(primary):017:0> result2
=> false

With the order of priority in thoughts, that is what occurs:

  • result2 is assigned the worth of false as a result of = happens earlier than or
  • The rest of the assertion is evaluated as true

This was hidden when true and false was swapped as a result of result2 was assigned a worth of true after which the rest of the assertion was additionally evaluated to true.

How do you stop this?

There may be only a few causes to make the most of the English phrases and and or in Ruby’s logical evaluations. As a substitute, these needs to be utilized as movement management modifiers (assume if and until) and never for boolean logic.

Here is an instance of the way it needs to be utilized for movement management

irb(primary):019:0> numeral = 10 && numeral / 5
(irb):19:in `<primary>': undefined technique `/' for nil:NilClass (NoMethodError)
        from C:/Ruby32-x64/lib/ruby/gems/3.2.0/gems/irb-1.6.2/exe/irb:11:in `<prime (required)>'
        from C:/Ruby32-x64/bin/irb:33:in `load'
        from C:/Ruby32-x64/bin/irb:33:in `<primary>'
irb(primary):020:0> numeral = 10 and numeral / 5
=> 2

In our first instance, with &&, the assertion is damaged up like this

numeral = (10 && numeral) / 5

This would possibly not work as a result of numeral would not have a worth when && is evaluated. Distinction this with the second model the place the code:

  • Assigns a worth of 10 to numeral
  • Divides numeral by 5

Whereas primary, it ought to get the purpose throughout. One other instance could possibly be one thing like this:

consumer = Person.find_by_email(electronic mail) and consumer.send_email!

Right here, we’re solely sending an electronic mail to the consumer if we discover their data. No data, no electronic mail is shipped.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles