Ruby 2.5.0 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > NameErrorクラス > local_variables

instance method NameError#local_variables

local_variables -> [Symbol][permalink][rdoc]

self が発生した時に定義されていたローカル変数名の一覧を返します。

内部での使用に限ります。

例:

def foo
  begin
    b = "bar"
    c = 123
    d
  rescue NameError => err
    p err.local_variables #=> [:b, :c, :err]
  end
end

a = "buz"
foo