Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个场景
func1 do x='abc' func2 do puts x end end
为此,我得到 x = nil。为什么会这样,以及如何访问内部块中的 x 等外部变量。
仅当 func1 和 func2 执行您传递给它们的块(屈服或调用)时,您才会得到“abc”。
查看一个示例
def func1 end def func2 end func1 do x = "Hello World" func2 do puts x end end #=> nil def func3 yield end def func4 yield end func3 do x = "Hello World" func4 do puts x end end #=> Hello World