2013-02-06 75 views
0

我在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錯誤。見下面的答案。

+0

tax_rate的定義在哪裏? – cymorg

+0

只要在tax_totals – Shinigami

回答

1

如上所述,這是有效的Ruby。我能夠將你的代碼放入一個方法中並調用它。見下:

require 'active_support/all' 
require 'rspec' 

class Category 
    def self.find(category_id) 
    [new] 
    end 

    def tax_rate 
    0.5 
    end 
end 

class Rate 
    def self.where(*args) 
    [new] 
    end 

    def tax_rate 
    0.5 
    end 
end 

def ratio(expenses, tax_rate) 
    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 
end 


describe "#ratio" do 

    let(:expense) do 
    double("expense", category_id: 5, rate_id: 6, transaction_date: 5.days.ago, amount: 5) 
    end 
    let(:expenses) { [expense] } 
    let(:tax_rate) { 0.25 } 

    it "should run" do 
    ratio(expenses, tax_rate) 
    end 
end 
+0

之上,那些類別和費率類別就非常棒! :o從來沒有見過這樣做。但不應該找到方法返回只有一個記錄,而不是一個數組?除非這是一箇舊的rails版本。 –

+0

啊......你說得對。那曾經是'where'。嘗試找到是一個絕望的措施。 – Shinigami

0

我是Ruby和Rails的新手,對我來說這是最古怪的事情。

錯誤來自於這樣一條天真的線條,我甚至都懶得把它包含在我原來的問題中。

tax_rate是一個正在傳遞給該方法的變量。它以整數存儲在數據庫中,所以我需要將它轉換爲小數點。這是更完整的代碼:

tax_rate = tax_rate.to_f /100 
    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 

而第一行是Ruby不喜歡,我仍然不知道爲什麼。但是你不能把myVar /100它必須是myVar/100甚至myVar/ 100但在/和數字之間必須有一個空格。

+0

因此,下一次,在您提出錯誤之前,請簡化您的代碼,以便所有不必要的變量和常量都消失,並且只保留裸露骨骼錯誤的代碼。那樣,你經常發現錯誤而不必問。 –