How can I check whether a variable is defined in Ruby?
In Ruby, you may sometimes need to know if a variable has been defined before attempting to use it. Attempting to call or reference an undefined variable can lead to errors. Fortunately, Ruby provides a built-in keyword, defined?
, that you can use to check whether a variable, constant, or method exists before accessing it.
1. Using the defined?
Keyword
The defined?
keyword returns nil
if the expression is not defined, or a descriptive string (such as "local-variable"
, "method"
, "constant"
, etc.) if it is defined. Here’s how to use it with different variable types:
Local Variables
if defined?(my_var) puts "my_var is defined!" else puts "my_var is not defined!" end
If my_var
exists in the current scope, defined?(my_var)
might return "local-variable"
or a similar string; otherwise, it returns nil
.
Instance Variables
@instance_var = 10 if defined?(@instance_var) puts "@instance_var is defined." end
Constants
MY_CONSTANT = 42 puts defined?(MY_CONSTANT) # => "constant"
Methods
def foo; end puts defined?(foo) # => "method"
2. Checking Variables of Different Types
- Global Variable: If you have a global variable like
$global_var
,defined?($global_var)
will return"global-variable"
if it’s defined. - Class/Module Variables: Similarly, checking class variables (
@@class_var
) or module variables uses the samedefined?
approach.
3. Common Pitfalls
-
nil?
vs.defined?
nil?
checks if a variable’s value isnil
(i.e., you’ve assignedmy_var = nil
).defined?
checks if a variable, method, or constant exists at all in the current scope.
my_var = nil puts "nil?" if my_var.nil? # => "nil?" puts "defined?" if defined?(my_var) # => "defined?"
-
Using
defined?
on Expressions
defined?
can also check expressions like1 + 2
, returning"expression"
, but that’s less common in practice. -
Scoping
Remember that local variables need to be in scope. If you try to reference a variable from outside its scope,defined?
will returnnil
.
4. Example Usage in Practice
if defined?(some_var) puts "some_var is defined as: #{some_var.inspect}" else puts "some_var is not defined." end
- If
some_var
was never declared, you’ll see “some_var is not defined.” - If
some_var
exists (even if it’snil
), you’ll see its value printed viainspect
.
Further Learning
If you’re sharpening your Ruby skills for job interviews or aiming to build robust Ruby applications, here are some recommended courses from DesignGurus.io:
-
Grokking the Coding Interview: Patterns for Coding Questions
Master essential coding patterns that appear frequently in FAANG-level interviews. -
Grokking System Design Fundamentals
Gain critical insights into designing scalable, distributed systems—a must-have for advanced Rails or backend engineers.
For personalized feedback from ex-FAANG engineers, explore Coding Mock Interview or System Design Mock Interview sessions.
Conclusion
To check if a variable (or method, constant, etc.) is defined in Ruby, use the built-in defined?
keyword. It returns a descriptive string if the entity exists—or nil
if it does not. This approach helps you write safer, more defensive code, avoiding errors when referencing uninitialized or out-of-scope variables.