How to check if a variable is nil in Ruby? Last Updated : 28 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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? methodUsing == operatorUsing unless statementUsing nil? methodThe nil? method is used to explicitly check if a variable is nil. It returns true if the object isnil, otherwise false. Syntax: variable.nil? Example: Below is the Ruby program to check if a variable is nil using nil? method: Ruby # Ruby program to check if a variable # is nil using nil? method # Example variable variable = nil if variable.nil? puts "Variable is nil" else puts "Variable is not nil" end OutputVariable is nil Explanation: In this example, we use the nil? method to explicitly check if the variable is nil. If variable is nil, it prints "Variable is nil", otherwise "Variable is not nil". Using == operator 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 # 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 OutputVariable 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". Using unless statementThe unless statement is the opposite of the if statement. It executes a block of code if the condition evaluates to false or nil. Syntax: unless variable # Code to execute if variable is nil end Example: Below is the Ruby program to check if a variable is nil using unless statement: Ruby # Ruby program to check if a variable # is nil using unless statement # Example variable variable = nil unless variable puts "Variable is nil" else puts "Variable is not nil" end OutputVariable is nil Explanation: In this example we use unless statement to execute a block of code if variable evaluates to false or nil. If variable is nil, it prints "Variable is nil", otherwise "Variable is not nil". Comment More infoAdvertise with us Next Article How to check if a variable is nil in Ruby? A abhay94517 Follow Improve Article Tags : Ruby Similar Reads How to check if a value exists in an Array in Ruby? In this article, we will discuss how to check if a value exists in an array in ruby. We can check if a value exists in an array through different methods ranging from using Array#member? method and Array#include? method to Array#any? method Table of Content Check if a value exists in an array using 3 min read How to Check the Data Type of a Variable in Ruby? In this article, we will discuss how to check the data type of a variable in Ruby. We can check the data types of a variable through different methods ranging from class to kind_of method in Ruby. Table of Content Using Class method Using is_a? Method Using kind_of? MethodUsing Class method The Clas 3 min read How to use JavaScript Variables in Ruby? JavaScript and Ruby are two powerful programming languages used extensively in web development. While each language has its own set of features and capabilities, there are times when developers may need to integrate functionalities from one language into the other. One common scenario is using JavaS 4 min read How to check if a file exists in Ruby? This article will discuss how to check if a file exists in Ruby. Checking if a file exists is important whether you are managing file operations, creating applications or handling data. The intention of this guide however is to offer a detailed account of methods that can be used to effectively achi 2 min read How to Determine if a Number is Prime in Ruby? In this article, we will discuss how to Determine if a Number is Prime and contains a specific value in ruby. We can determine if a Number is Prime through different methods. Let us study them in detail Table of Content Determine if a Number is Prime using Prime Library ApproachDetermine if a Number 3 min read Instance Variables in Ruby There are four different types of variables in Ruby- Local variables, Instance variables, Class variables and Global variables. An instance variable in ruby has a name starting with @ symbol, and its content is restricted to whatever the object itself refers to. Two separate objects, even though the 3 min read True, False, and Nil In Ruby Ruby is an open-sourced object-oriented programming language developed by Yukihiro Matsumoto. In Ruby, everything is treated as an object. true, false and nil are built-in data types of Ruby. Note: Always remember in Ruby true, false, and nil are objects, not numbers. Whenever Ruby requires a Boolea 2 min read How to check pointer or interface is nil or not in Golang? In Golang, nil check is frequently seen in GoLang code especially for error check. In most cases, nil check is straight forward, but in interface case, it's a bit different and special care needs to be taken. Here the task is to check pointer or interface is nil or not in Golang, you can check with 3 min read Ruby | Types of Variables There are different types of variables in Ruby: Local variables Instance variables Class variables Global variables Each variable in Ruby is declared by using a special character at the start of the variable name which is mentioned in the following table: Symbol Type of Variable [a-z] or _ Local Var 4 min read Like