ZetCode

Ruby NOT Keyword

last modified October 18, 2026

This tutorial explains how to use Ruby's not keyword for logical negation. The not operator reverses boolean values in expressions.

The not keyword is a logical operator that returns the opposite of its operand. It has lower precedence than !, which affects how expressions are grouped.

not works with truthy and falsey values in Ruby. It evaluates the truthiness of its operand and returns a boolean. Parentheses are often useful for clarity.

Basic NOT Example

This simple example shows the fundamental behavior of the not operator. It reverses boolean values.

basic_not.rb
flag = true
puts not(flag)  # Outputs false

flag = false
puts not(flag)  # Outputs true

The not operator returns false when given true and vice versa. In this form, parentheses are required around the operand.

NOT with Truthy Values

Ruby considers many values truthy besides true. The not operator handles these consistently.

truthy_values.rb
puts not("hello")  # false
puts not(42)       # false
puts not([])       # false
puts not(nil)      # true
puts not(false)    # true

Only nil and false evaluate as falsey in Ruby. All other values are truthy.

NOT in Conditional Statements

The not operator is commonly used in conditional expressions. This example shows it controlling program flow.

conditional.rb
logged_in = false

if not logged_in
  puts "Please log in"
else
  puts "Welcome back"
end

The condition checks if the user is not logged in. The not makes the logic more readable than ! in this case.

NOT with Comparison Operators

Because not has lower precedence than comparison operators, it applies to the whole comparison. Parentheses can make that grouping explicit. This example demonstrates the syntax.

comparison.rb
age = 17

if not (age >= 18)
  puts "You're not old enough"
end

not already applies to the entire comparison without the parentheses. They are optional here, but they make the intent clearer.

NOT vs. Bang Operator

This example contrasts not with ! to show their differences in precedence and behavior.

not_vs_bang.rb
x = true
y = false

puts !x && y    # false: parsed as (!x) && y
puts not(x && y) # true: x && y is evaluated, then negated

! binds more tightly than &&, so !x is evaluated first. not binds less tightly than &&, so it negates the result of the whole expression. The parentheses above make that grouping explicit.

NOT in Method Definitions

Methods can return negated results using not. This example shows a validation method.

method_definition.rb
def invalid_email?(email)
  not (email.include?('@') && email.end_with?('.com'))
end

puts invalid_email?('test@example.com') # false
puts invalid_email?('bad_email')        # true

The method returns true for invalid emails. The not clearly expresses the inverse of valid email criteria.

NOT with Nil Checks

The not operator works well for nil checks, providing readable code. This example demonstrates nil handling.

nil_check.rb
user = nil

if not user
  puts "User not found"
end

# Safe navigation returns nil for a nil receiver
name = user&.name || "Guest"
puts name

The not operator checks for nil or false values. The safe navigation operator returns nil for a nil receiver, so || can supply a fallback.

Source

Ruby Keywords Documentation

This tutorial covered Ruby's not keyword with practical examples showing boolean negation, conditionals, and comparisons.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Ruby tutorials.