Explain

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"

Recommended Courses

Methods

def foo; end
puts defined?(foo)  # => "method"

2. Checking Variables of Different Types

  1. Global Variable: If you have a global variable like $global_var, defined?($global_var) will return "global-variable" if it’s defined.
  2. Class/Module Variables: Similarly, checking class variables (@@class_var) or module variables uses the same defined? approach.

3. Common Pitfalls

  1. nil? vs. defined?

    • nil? checks if a variable’s value is nil (i.e., you’ve assigned my_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?"
    
  2. Using defined? on Expressions
    defined? can also check expressions like 1 + 2, returning "expression", but that’s less common in practice.

  3. Scoping
    Remember that local variables need to be in scope. If you try to reference a variable from outside its scope, defined? will return nil.

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’s nil), you’ll see its value printed via inspect.

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:

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.