1

Currently if it doesn't exists I get a the undefined local variable or method error.

How can I check the value of the variable and also account for it not existing at all.

I thought && was the deal but:

if defined? aaa && aaa == '123' then puts aaa end

NameError: undefined local variable or method `aaa' for main:Object

1 Answer 1

8

In this case, you need the parenthetical like defined?(aaa) otherwise it is evaluating the entire expression aaa && aaa == '123' as if it were defined?(aaa && aaa == '123'). So your code is really doing this:

if defined?(aaa && aaa == '123') # returns "expression" string, and thus true
  puts aaa # the error comes from this part, since aaa is not defined.
end
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.