Ruby 2.2.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Bindingクラス > local_variable_defined?
local_variable_defined?(symbol) -> bool
[permalink][rdoc]引数 symbol で指定した名前のローカル変数が定義されている場合に true を、そうでない場合に false を返します。
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