All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is the difference between nil and false in Ruby?

Post cover: What is the difference between nil and false in Ruby?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
In Ruby, both nil and false are used to represent the concept of "nothing" or "falseness," but they have different roles and behaviors. Understanding the differences between them is important for writing clear and effective Ruby code.

Characteristics of nil value

  • Type: nil is an instance of the NilClass.
  • Value: Represents the absence of a value or "nothing." It is often used to indicate that a variable has not been assigned any value or that a method did not return any result.
  • Truthiness: nil is considered "false" in conditional expressions. This means that in an if statement, nil will be treated as a false value.
For example:
value = nil
if value
  puts "This code will not be executed"
else
  puts "The variable value is nil"
end
Result:
The variable value is nil
=> nil

Characteristics of false value

Type: false is an instance of the FalseClass.
Value: Represents a logical false value. Used in logical operations to indicate that a condition is false.
Truthiness: false is considered "false" in conditional expressions. This means that in an if statement, false will be treated as a false value.
For example:
value = false
if value
  puts "This code will not be executed"
else
  puts "The variable value is false"
end
Result:
The variable value is false
=> nil
Why do we see nil after the printed text? Read in the post - Why does Ruby code return nil after executing puts?
The main difference between nil and false lies in their meaning and usage. nil signifies the absence of a value or "nothing," while false indicates logical falseness. Both values are considered false in conditional expressions, but their meanings and uses differ. This allows distinguishing situations where a variable has no value (nil) and where a condition is logically false (false).
That is, nil is used to describe emptiness. False is used to denote falseness.
  • If we need to return a false result for the validation is_valid?, we should return false.
  • If we need to clear a value - we use nil.
  • And in a conditional expression, both nil and false yield a false result.
Let's check:
def check_value(value)
  if value
    puts "The value is true"
  else
    puts "The value is false (nil or false)"  # This will be printed for nil and false
  end
end

check_value(nil)    # Will print: The value is false (nil or false)
check_value(false)  # Will print: The value is false (nil or false)
check_value(true)   # Will print: The value is true
check_value(1)      # Will print: The value is true
check_value("Hello")# Will print: The value is true
In the characteristics of values, we mentioned NilClass and FalseClass. Personally, I have never seen these classes touched upon when working with Ruby, but let's consider them for purely academic purposes.

FalseClass

FalseClass is a built-in class in Ruby that represents logical falseness. In Ruby, only two values are considered false in conditional expressions: false and nil. Here are some key points:
  • In Ruby, false is the only instance of the FalseClass. This means that all variables that have the value false refer to the same object. More details about immediate values have been written here.
  • The value false is often used in logical operations and conditional expressions to denote a false condition.
  • The FalseClass has several methods, among which the most common are &, |, and ^ for performing logical operations.
FalseClass Instance methods: &, ===, ^, inspect, to_s, |
FalseClass Instance methods: &, ===, ^, inspect, to_s, |
Example of interaction with FalseClass
is_valid = false

puts is_valid.class # => FalseClass

# Usage in a conditional expression
if is_valid
  puts "This code will not be executed"
else
  puts "is_valid is false" # This will be printed
end

# Logical operations
puts false & true  # => false
puts false | true  # => true
puts false ^ true  # => true
Thus, FalseClass holds the value false and also has methods for performing logical operations.

NilClass

NilClass is also a built-in class in Ruby that represents the absence of a value or "nothing."
  •  In Ruby, nil is the only instance of the NilClass. This means that all variables that have the value nil refer to the same object.
  •  The value nil is often used to denote the absence of a value or an undefined state.
  • The NilClass has several methods, including nil?, which always returns true for the nil object, as well as methods for converting to other types (to_s, to_i, to_f).
    NilClass Instance methods: &, ===, =~, ^, inspect, nil?, rationalize, to_a, to_c, to_d, to_f, to_h, to_i, to_r, to_s, |
    NilClass Instance methods: &, ===, =~, ^, inspect, nil?, rationalize, to_a, to_c, to_d, to_f, to_h, to_i, to_r, to_s, |
Example of interaction with NilClass
value = nil

puts value.class # => NilClass

# Usage in a conditional expression
if value
  puts "This code will not be executed"
else
  puts "value is nil" # This will be printed
end

# NilClass methods
puts nil.nil?  # => true
puts nil.to_s  # => ""
puts nil.to_i  # => 0
puts nil.to_f  # => 0.0
NilClass is needed for working with the nil value (similarly to FalseClass).

This post doesn't have any additions from the author yet.

How does 'rails console --sandbox' work?
23 May 19:39

How does 'rails console --sandbox' work?

meme code
meme code@memecode
What is the purpose of the CVE (Common Vulnerabilities and Exposures) database?
29 May 08:05

What is the purpose of the CVE (Common Vulnerabilities and Exposures) database?

meme code
meme code@memecode
29 May 09:09

Which operating systems support Ruby?

meme code
meme code@memecode
Does Ruby create a new copy of an object when assigning a variable to another variable?
29 May 09:30

Does Ruby create a new copy of an object when assigning a variable to another variable?

meme code
meme code@memecode
What is the difference between immediate value and reference in Ruby?
29 May 12:00

What is the difference between immediate value and reference in Ruby?

meme code
meme code@memecode
Why does Ruby code return nil after executing puts?
29 May 20:30

Why does Ruby code return nil after executing puts?

meme code
meme code@memecode
Why is an empty string in Ruby not false?
31 May 14:39

Why is an empty string in Ruby not false?

meme code
meme code@memecode
The scope of a local variable in Ruby
03 Jun 16:46

The scope of a local variable in Ruby

meme code
meme code@memecode
What is the difference between int and bigint in Ruby? Minimum and maximum values.
13 Jun 06:37

What is the difference between int and bigint in Ruby? Minimum and maximum values.

meme code
meme code@memecode
What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes
13 Jun 07:18

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with a limit of 4 bytes

meme code
meme code@memecode
What is immutability and mutability?
19 Jun 07:48

What is immutability and mutability?

meme code
meme code@memecode
What will be the result of adding 10.5 and 10?
23 Jun 13:23

What will be the result of adding 10.5 and 10?

meme code
meme code@memecode