Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable exists with a value without "undefined local variable or method"?

This is a common pattern: If a variable doesn't exist I get an undefined local variable or method error.

The existing code has if variable_name.present? but this didn't account for the variable not existing.

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

I've tried:

if (defined? mmm) then
  if mmm.present? then
    puts "true"
  end
end

but Ruby still checks that inner mmm.present? and throws "no such variable" when it doesn't exist.

I'm sure there's a common pattern/solution to this.

like image 528
Michael Durrant Avatar asked Aug 24 '12 13:08

Michael Durrant


People also ask

How do you check if a variable exists?

Answer: Use the typeof operator If you want to check whether a variable has been initialized or defined (i.e. test whether a variable has been declared and assigned a value) you can use the typeof operator.

How do you check if a variable is nil in Ruby?

That's the easy part. In Ruby, you can check if an object is nil, just by calling the nil? on the object... even if the object is nil. That's quite logical if you think about it :) Side note : in Ruby, by convention, every method that ends with a question mark is designed to return a boolean (true or false).


2 Answers

Change the present? to != '' and use the && operator which only tries to evaluate the seond expression if the first one is true:

if defined?(mmm) && (mmm != '') then puts "yes" end

But actually as of 2019 this is no longer needed as both the below work

irb(main):001:0> if (defined? mm) then
irb(main):002:1* if mm.present? then
irb(main):003:2* p true
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> if (defined? mm) then
irb(main):007:1* p mm
irb(main):008:1> end
=> nil
like image 63
Michael Durrant Avatar answered Oct 04 '22 00:10

Michael Durrant


On Ruby on Rails

if defined?(mm) && mm.present?
  puts "acceptable variable"
end

On IRB

if defined?(mm) && !mm.blank? && !mm.nil?
  puts "acceptable variable"
end

It can make sure you won't get undefined variable or nil or empty value.

Understand how defined? works

a = 1
defined?(a) # => "local-variable"

b = nil
defined?(b) # => "local-variable"

c = ""
defined?(c) # => "local-variable"

d = []
defined?(d) # => "local-variable"

$e = 'text'
defined?($e) # => "global-variable"

defined?(f) # => nil
defined?($g) # => nil

Note that defined? checks variable in the scope it is.

Why you need defined?

When there is possible of undefined variable presence, you cannot just check it with only .nil? for eaxample, you will have a chance to get NameError.

a = nil
a.nil? # => true
b.nil? # => NameError: undefined local variable or method `b'
like image 21
Komsun K. Avatar answered Oct 04 '22 00:10

Komsun K.