Last active
September 11, 2021 14:44
-
-
Save teslur/9c88168992c2ba9e3fc2c21ec5d4cf66 to your computer and use it in GitHub Desktop.
hoisting in debugger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'bundler/setup' | |
require 'debug' | |
def method_or_variable | |
'method' | |
end | |
if true | |
binding.break # (rdbg) p method_or_variable #=> nil | |
else | |
method_or_variable = 'variable' | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def method_or_variable | |
'method' | |
end | |
if true | |
p method_or_variable #=> "method" | |
else | |
method_or_variable = 'variable' | |
end |
昔モチャモチャやったリポにも反映した。
hoistディレクトリ配下だけ反映。
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
通常のrubyプログラムとしてnormal.rbを実行すると、
"method"
が出力される。この
"method"
を出力する位置にbinding.break
を仕込んだdebugger.rbを実行し、debug gem内でp method_or_variable
を行うとnil
が出力される。下のローカル変数がhoistingを起こしていて、メソッド呼び出しよりもローカル変数が優先される結果、ローカル変数を参照している。
これは別にdebug gemだからじゃなくて、
binding.irb
でもbinding.pry
でもなんならeval(:method_or_variable)
でも同じことが起きる。