在这个例子中,
def foo(x)
if(x > 5)
bar = 100
end
puts bar
end
Run Code Online (Sandbox Code Playgroud)
然后foo(6)输出:100和foo(3)什么都不输出.
但是,如果我将定义更改为
def foo(x)
if(x > 5)
bar = 100
end
puts bob
end
Run Code Online (Sandbox Code Playgroud)
我得到一个"未定义的局部变量或方法"错误.
所以我的问题是为什么当我调用foo(3)并且从未设置bar时我没有收到此错误?
ruby ×1