Я написал сценарий для выполнения некоторых расчетов, которые я ранее имел в электронной таблице. Вот мой код:Ruby Name Error
#Ruby
require '../file/fromFile'
input = commaSep('gen.txt')
civil = input[0]
economy = input[1]
political = input[2]
tax = input[3]
taxMod = input[4]
production = input[5]
population = input[6]
admin = input[7]
#debug puts
puts civil
puts economy
puts political
puts tax
puts taxMod
puts production
puts population
puts admin
TOUT_CONST = 5076287515610300
def workerEnthusiasm()
1 + civil + political + taxMod
end
def consumerConfidence()
1 + economy + civil
end
def adminBudgetCurve()
if admin - 0.3 > 0 then
signAdmin = 1
elsif admin - 0.3 == 0 then
signAdmin = 0
elsif admin - 0.3 < 0 then
signAdmin = -1
end
return 1 - admin * signAdmin * ((admin - 0.3).abs ** (1/3)) + 0.2 + admin
end
def govEfficiency()
1 + political/adminBudgetCurve()
end
def output()
production * population * workerEnthusiasm() * consumerConfidence()
end
def consumption()
output() * (1 - tax)
end
def govBudget()
output() * govEfficiency() * (tax + consumerConfidence()/10 + workerEnthusiasm()/40)
end
def govExpenditure()
govBudget() * govEfficiency()
end
def govWaste()
govBudget() - govExpenditure()
end
def imports()
((1/consumerConfidence())/8) * (consumption() + govExpenditure())
end
def exports()
64970419915908 * (output()/TOUT_CONST)
end
def netTrade()
exports() - imports()
end
def GDP()
govBudget() + netTrade() + govExpenditure
end
def GDPPC()
GDP()/population
end
def exchangeRate()
Math.sqrt(GDPPC() * production/40400000)
end
def unemployment()
((GDPPC() - 37500) ** 2) * 1.25 * (10 ** -10) - 1.5 * (10 ** -6) * (GDPPC() - 37500) + 0.03
end
puts unemployment()
Когда я запускаю код, я получаю NameError. Вот терминал вывода:
0.03
0.03
0.01
0.25
0.04
30000
241401636
0.05
gen.rb:45:in `output': undefined local variable or method `production' for main:Object (NameError)
from gen.rb:51:in `govBudget'
from gen.rb:69:in `GDP'
from gen.rb:72:in `GDPPC'
from gen.rb:78:in `unemployment'
from gen.rb:81:in `<main>'
Похоже, ошибка в функции вывода, но я не могу понять, почему.
Когда вы ссылаетесь на возбужденное исключение, важно предоставить всю соответствующую информацию. Здесь, что вызвало исключение 'NameError' и в какой строке кода это произошло? Тщательное прочтение сообщения об исключении часто укажет на проблему. –