第 6 行出现错误。我尝试打印 ruby 解释器无法识别的全局变量。这是为什么 ?我该如何修改?
localVar = "Local Variable"
$globalVar = "Global Variable"
puts(localVar)
def func
puts("From the function func")
puts(globalVar) #line 6
end
def changer
globalVar = "New Global Var"
puts("New Global Var : #{globalVar}")
end
func #Call the function func
changer #Call the function changer
输出 :
Local Variable
From the function func
global.rb:6:in `func': undefined local variable or method `globalVar' for main:Object (NameError)
from global.rb:13:in `<main>'