我在Ruby中有一個簡單的財務應用程序,它可以跟蹤用戶的開支並基於它們生成報表。簡單的計算導致Ruby中的語法錯誤
費用屬於不同類別,這會影響每項費用的稅額。
在我的代碼生成的開支報告,我有這片:
tax_totals = [0] * 13
totals = [0] * 13
expenses.each do |expense|
tax_ratio = tax_rate/(1+tax_rate)
category = Category.find(expense.category_id).first
tax_ratio *= category.tax_rate.to_f/100
if !expense.rate_id.nil?
subcategory = Rate.where("id = ?", expense.rate_id).first
tax_ratio *= subcategory.tax_rate.to_f
end
tax_totals[expense.transaction_date.to_date.month] +=
(expense.amount * tax_ratio)
totals[expense.transaction_date.to_date.month] += expense.amount
end
我一直就行了tax_ratio = tax_rate/(1+tax_rate)
得到一個語法錯誤:
syntax error, unexpected '(', expecting keyword_end
如果我刪除了這一行,將問題轉移到tax_ratio *= category.tax_rate.to_f/100
行:
syntax error, unexpected tINTEGER, expecting keyword_end
,我也沒辦法,磨片這是從何而來。我根本沒有看到代碼有任何問題。我在多個函數中有非常相似的代碼,每個函數的計算方式都略有不同但只有這一個是一個問題。
也許是缺乏咖啡因。這段代碼有什麼問題嗎?有沒有其他的文件導致這種情況?我如何繼續調試?
乾杯!
編輯:我想通了。 Ruby noob錯誤。見下面的答案。
tax_rate的定義在哪裏? – cymorg
只要在tax_totals – Shinigami