Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Binding#local_variable_defined?

binding.local_variable_defined?(symbol) → obj

Returns true if a local variable symbol exists.

Example
def foo
  a = 1
  binding.local_variable_defined?(:a) #=> true
  binding.local_variable_defined?(:b) #=> false
end

This method is the short version of the following code:

Example
binding.eval("defined?(#{symbol}) == 'local-variable'")
See also #irb #local_variable_get #eval #local_variables also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0