我有一个变量,我设置了默认值,然后对其执行了一个过程。问题在于,在块内部我无法访问该变量,因为该块具有自己的变量作用域并且无法访问外部。以下是代码片段:
value = ""
cmd_errors = Array.new
# Call the command line
status = POpen4.popen4(cmd) do |stdout, stderr|
output = stdout.read
error = stderr.read
if (!output.empty?)
value = JSON.parse(output) #This just creates a block scoped variable called 'value' and my local variable is still empty
else
cmd_errors << error
end
end
是否可以允许该块写入该本地变量?也许可以使用引用?
p output,error
吗?似乎if
条件从未为真。 - undefinedp x
相当于puts x.inspect
,所以你不应该写成p value.inspect
,因为它和puts x.inspect.inspect
是一样的,会造成很糟糕的结果。 - undefined