Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. 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'")

Ruby version changes

  1. 4.0