Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

How do you check whether a variable is defined in Ruby? Is there an "isset"-type method available?

share|improve this question

8 Answers

up vote 246 down vote accepted

Use the defined? keyword (documentation). It will return a String with the kind of the item, or nil if it doesn’t exist.

>> a = 1
 => 1
>> defined? a
 => "local-variable"
>> defined? b
 => nil
>> defined? nil
 => "nil"
>> defined? String
 => "constant"
>> defined? 1
 => "expression"

As skalee commented: "It is worth noting that variable which is set to nil is initialized."

>> n = nil  
>> defined? n
 => "local-variable"
share|improve this answer
28  
It is worth noting that variable which is set to nil is initialized. – skalee Feb 16 '11 at 13:18
3  
If you want to set a variable if it doesn't exist and leave it alone if it does, see @danmayer's answer (involving the ||= operator) below. – jrdioko Jul 12 '11 at 21:29

this is useful if you want to do nothing if it does exist but create it if it doesn't exist

def get_var
  @var ||= SomeClass.new()
end

this only creates the new class once after that just keeps returning the var

share|improve this answer
4  
This is very idiomatic Ruby too and very typical, by the way. – jrdioko Jul 12 '11 at 21:28
Thanks for this input ! I'm a ruby/rails beginner and came accross this syntax yesterday evening, had a hard time figuring out what it meant. Looks very handy ! – LePad Nov 7 '11 at 10:35
this won't work for defining constants – Ivailo Bardarov Nov 24 '11 at 14:52
9  
Just don't use ||= with boolean values, lest you feel the pain of confusion. – Andrew Marshall May 18 '12 at 6:27
2  
along with what @AndrewMarshall said, avoid this idiom with anything that might return nil as well unless you really want to evaluate the expression every time it's called when it does return nil – nzifnab Nov 19 '12 at 23:58

The correct syntax for the above statement is:

if (defined?(var)).nil? # will now return true or false
 print "var is not defined\n".color(:red)
else
 print "var is defined\n".color(:green)
end

substituting (var) with your variable. This syntax will return a true/false value for evaluation in the if statement.

share|improve this answer
1  
That's not necessary as nil evaluates to false when used in a test – Jerome Apr 5 '12 at 14:23
Why not defined?(var) == nil ? – vol7ron Apr 23 at 22:17

defined?(your_var) will work. Depending on what you're doing you can also do something like your_var.nil?

share|improve this answer

Try "unless" instead of "if"

a = "apple"
# Note that b is not declared
c = nil

unless defined? a
    puts "a is not defined"
end

unless defined? b
    puts "b is not defined"
end

unless defined? c
    puts "c is not defined"
end
share|improve this answer
What does this answer add that hasn't been said by the other answers? – Andrew Grimm May 31 '12 at 22:54
2  
It very nicely answers the question in one more useful way, doesn't it? – look Aug 9 '12 at 22:54

Here is some code, nothing rocket science but it works well enough

require 'rubygems'
require 'rainbow'
if defined?(var).nil?
 print "var is not defined\n".color(:red)
else
 print "car is defined\n".color(:green)
end
share|improve this answer
Why the down vote?... – Sardathrion Apr 17 '12 at 11:59
Presumably because the nil? is optional. – James Jun 18 '12 at 16:15
@James: really? Downvote instead of edit and change?.... meh, never mind. ^_~ – Sardathrion Jun 20 '12 at 8:12
Blame whoever downvoted it back in April; I just commented. ;-) – James Jun 20 '12 at 13:47
@James: I do blame them, not you! ;) – Sardathrion Jun 20 '12 at 14:35

You can try:

unless defined?(var)
  #ruby code goes here
end
=> true

Because it returns a boolean.

share|improve this answer
SyntaxError: compile error (irb):2: syntax error, unexpected $end, expecting kEND – Andrew Grimm Aug 2 '12 at 23:11
to use an unless statement seems overly complicated – johannes Oct 29 '12 at 8:38

Use defined? YourVariable
Keep it simple silly .. ;)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.