我想知道,为什么a
可见?if语句中的局部变量
if true
puts 'true'
else
puts 'false'
a = 123
end
puts a # no error
# or
# my_hash = {key: a}
# puts my_hash # :key => nil
但是这将导致一个错误,即使将有“真”所示
if true
puts 'true'
else
puts 'false'
a = 123
end
puts a2 # boooooom
查看关于[此问题]的接受的答案(http://stackoverflow.com/questions/12928050/why-does-ruby-seem-to-hoist-variable-declarations-from-inside-a-case-statement- e?rq = 1#) –