Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Binding#local_variable_defined?

(::interned varname) -> bool
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.3 · 3.2 · 3.1 · 3.0