Skip to main content

How to Combine Conditions with AND (&&), OR (||) and NOT (!) in Ruby

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

In Ruby, && (and) is true only when both conditions are true, || (or) is true when at least one is, and ! (not) turns true into false and false into true. && and || stop as soon as they know the answer, so the right side can rely on the left.

Syntax​

Syntax
<condition> && <condition>
<condition> || <condition>
!<condition>
PartWhat it does
&&And: true only when both conditions are true. When the left one is false, the right one isn't evaluated.
||Or: true when at least one condition is true. When the left one is true, the right one isn't evaluated.
!Not: gives false for a true value, and true for a false one.
<condition>Any expression. Only nil and false count as false.

The Ruby documentation says && and and provide "short-circuiting by executing each side of the operator, left to right, and stopping at the first occurrence of a falsey expression", and that || and or short-circuit by returning "the result of the first expression that is truthy".

The Example​

The example checks three conditions about a user: whether they're signed in and not banned, whether their age gets a reduced ticket price (under 12, or 65 and over), and whether they aren't signed in. Then it checks for the admin role with include?, but only after is_a?(Array) confirms the roles are an array, because this user has no roles at all. Finally it calls include? without that check.

Prerequisites​

  • Ruby. This guide uses Ruby 4.0.10.

Step 1: Write the Code​

Create a file named main.rb:

main.rb, which sets signed_in to true, banned to false and age to 70, prints signed_in and not banned, age under 12 or at least 65, and not signed_in with p, then sets roles to nil and prints whether roles is an Array and includes admin.

main.rb
signed_in = true
banned = false
age = 70

# && is true only when both sides are true.
p signed_in && !banned
# || is true when at least one side is true.
p age < 12 || age >= 65
# ! turns true into false, and false into true.
p !signed_in

roles = nil # no roles at all
# && skips its right side when the left side is false, so include? never runs on nil.
p roles.is_a?(Array) && roles.include?('admin')

p prints each result. is_a?(Array) checks that roles is an array, and include?('admin') checks whether the array holds 'admin'.

Step 2: Run It​

Terminal
ruby main.rb
Output
true
true
false
false

The command is the same in zsh, bash and PowerShell.

Step 3: Remove the is_a? Check​

Replace main.rb with a version that calls include? on its own:

main.rb
roles = nil # no roles at all
# Without the is_a? check, include? runs on nil.
p roles.include?('admin')
Terminal
ruby main.rb
Output
main.rb:3:in '<main>': undefined method 'include?' for nil (NoMethodError)

p roles.include?('admin')
^^^^^^^^^

A zsh terminal where ruby main.rb prints true twice and false twice; for the changed file it stops with undefined method include? for nil (NoMethodError).

What Happened​

  • signed_in && !banned was true: both sides were true, because ! turned false into true.
  • age < 12 || age >= 65 was true: the left side was false, but the right side was true.
  • !signed_in was false.
  • roles.is_a?(Array) && roles.include?('admin') was false, and include? never ran. nil.is_a?(Array) was false, so && already knew the answer and skipped its right side.
  • Without the is_a? check, include? ran on nil and stopped the script with undefined method 'include?' for nil (NoMethodError).

Common Mistakes​

MistakeWhat happensDo this instead
Writing ok = true and falseok is true, because = runs before andUse &&, or put the and in parentheses
Writing !age >= 65 to mean "not 65 or over"! applies to age first and gives false, and false >= 65 stopped with undefined method '>=' for false (NoMethodError)Put the comparison in parentheses: !(age >= 65)
Mixing && and || without parentheses&& runs first: with a set to true and b and c to false, a || b && c was true, but (a || b) && c was falseAdd parentheses
Treating 0, '' or the string 'false' as falseThey all count as true: !0, !'' and !'false' were all falseCompare the value itself, as in count == 0

Troubleshooting​

SymptomLikely causeFix
undefined method 'include?' for nil (NoMethodError)include? ran on nilPut roles.is_a?(Array) && in front of it
undefined method '>=' for false (NoMethodError)! applied to a value before a comparisonPut the comparison in parentheses
A condition that's always true or always false!, and or or grouped the parts differently than you meantAdd parentheses

FAQ​

Which values count as false?​

Only nil and false. The Ruby documentation: "All objects except nil and false evaluate to a true value in conditional expressions." In my check, !nil and !false were true, while !0, !'', ![] and !'false' were all false.

Do && and || always return true or false?​

No. They return the last value they looked at, as the documentation puts it: "it’s the last evaluated expression that defines the result of the operation." In my check, nil && 'admin' gave nil, 'customer' && 'admin' gave "admin", and nil || 'guest' gave "guest". So name || 'guest' gives 'guest' when name is nil.

Are and and or the same as && and ||?​

They short-circuit the same way, but they have lower precedence, lower even than =: the precedence table lists && and || above =, and not, or and and below it. That's why ok = true and false left ok set to true, while ok = (true and false) set it to false.

Conclusion​

&&, || and ! combine conditions, and only nil and false count as false. Because && and || skip their right side once the answer is known, put a type check such as is_a?(Array) on the left of the code that relies on it, and use parentheses wherever the grouping isn't obvious.

Additional Resources​