How to Combine Conditions with AND (&&), OR (||) and NOT (!) in Ruby
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
<condition> && <condition>
<condition> || <condition>
!<condition>
| Part | What 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:

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
ruby main.rb
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:
roles = nil # no roles at all
# Without the is_a? check, include? runs on nil.
p roles.include?('admin')
ruby main.rb
main.rb:3:in '<main>': undefined method 'include?' for nil (NoMethodError)
p roles.include?('admin')
^^^^^^^^^

What Happened
signed_in && !bannedwastrue: both sides were true, because!turnedfalseintotrue.age < 12 || age >= 65wastrue: the left side wasfalse, but the right side wastrue.!signed_inwasfalse.roles.is_a?(Array) && roles.include?('admin')wasfalse, andinclude?never ran.nil.is_a?(Array)wasfalse, so&&already knew the answer and skipped its right side.- Without the
is_a?check,include?ran onniland stopped the script withundefined method 'include?' for nil (NoMethodError).
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing ok = true and false | ok is true, because = runs before and | Use &&, 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 false | Add parentheses |
Treating 0, '' or the string 'false' as false | They all count as true: !0, !'' and !'false' were all false | Compare the value itself, as in count == 0 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
undefined method 'include?' for nil (NoMethodError) | include? ran on nil | Put roles.is_a?(Array) && in front of it |
undefined method '>=' for false (NoMethodError) | ! applied to a value before a comparison | Put the comparison in parentheses |
| A condition that's always true or always false | !, and or or grouped the parts differently than you meant | Add 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
- Logical Operators, in the Ruby documentation.
- Precedence, in the Ruby documentation.
- BasicObject#!, in the Ruby documentation.
