How to use == operator In Ruby
The ==
operator is used to compare a variable with nil
. It returns true
if the variable is nil
, otherwise false
.
Syntax:
variable == nil
Example:
Below is the Ruby program to check if a variable is nil using == operator.
# Ruby program to check if a variable
# is nil using == operator
# Example variable
variable = nil
if variable == nil
puts "Variable is nil"
else
puts "Variable is not nil"
end
Output
Variable is nil
Explanation:
In this example, the ==
operator compares variable
with nil
. If variable
is equal to nil
, it prints “Variable is nil”, otherwise “Variable is not nil”.
How to check if a variable is nil in Ruby?
In Ruby, nil is used to
indicate that a variable has not been assigned a value or that a method call returned nothing. This article focuses on discussing how to check if a variable is nil in Ruby.
Table of Content
- Using nil? method
- Using == operator
- Using unless statement
Contact Us