Ruby 2.1.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Bindingクラス > local_variable_defined?

instance method Binding#local_variable_defined?

local_variable_defined?(symbol) -> bool[permalink][rdoc]

引数 symbol で指定した名前のローカル変数が定義されている場合に true を、そうでない場合に false を返します。

[PARAM] symbol:
ローカル変数名を Symbol オブジェクトで指定します。


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

このメソッドは以下のコードの短縮形です。


binding.eval("defined?(#{symbol}) == 'local-variable'")

[SEE_ALSO] Binding#local_variable_get, Binding#local_variable_set