ZetCode

Ruby NOT Keyword

last modified April 27, 2025

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 ! but is more readable.

not works with truthy and falsey values in Ruby. It converts its operand to boolean before negation. Parentheses are often needed 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. Parentheses make the expression clearer.

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 become true when converted to boolean.

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

Combining not with comparison operators requires parentheses. This example demonstrates proper syntax.

comparison.rb
age = 17

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

Without parentheses, Ruby would parse this differently due to operator precedence. The not applies to the entire comparison.

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 (&& evaluated first)
puts not(x && y) # true (not evaluated first)

The ! has higher precedence than &&, while not has lower precedence. This affects evaluation order.

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

# Alternative with safe navigation
user&.name || puts("User not found")

The not operator cleanly checks for nil or false values. The safe navigation operator provides an alternative approach.

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.