Ruby on rails Ruby中的简单计算导致语法错误
我在Ruby中有一个简单的财务应用程序,它可以跟踪用户的支出并根据这些支出生成报告 费用属于不同的类别,这影响到每项费用中有多少是税款 在生成费用报告的代码中,我有以下内容:Ruby on rails Ruby中的简单计算导致语法错误,ruby-on-rails,ruby,syntax-error,Ruby On Rails,Ruby,Syntax Error,我在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).f
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\u ratio=tax\u rate/(1+tax\u rate)
一行中不断遇到语法错误:
如果我删除该行,错误将移动到tax\u ratio*=category.tax\u rate.to\u f/100
行:
syntax error, unexpected tINTEGER, expecting keyword_end
我不知道这是从哪里来的。我看不出代码有任何问题。我在多个函数中有非常相似的代码,每个函数的计算方式略有不同。但只有这一个是个问题
也许是因为缺少咖啡因。这个代码有什么问题吗?文件中是否有其他原因导致此问题?如何继续调试
干杯
编辑:我想出来了。鲁比·努布错了。请参阅下面的答案。如上所述,这是有效的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
我是Ruby和Rails的新手,对我来说这是有史以来最奇怪的事情 这个错误来自一条看起来很无辜的线,我甚至懒得把它包括在我原来的问题中
tax\u rate
是传递给方法的变量。它以整数形式存储在DB中,所以我需要将它转换为小数点。这是更完整的代码:
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
,但是在/
和数字之间绝对需要一个空格 税率定义在哪里?就在税收总额之上,这些类别和税率类别非常棒我从没见过它这样做。但是find方法不应该只返回一条记录而不是一个数组吗?除非这是一个旧的rails版本。啊。。。你说得对。那以前是“where”。尝试查找是一个不顾一切的措施。因此,下一次,在您询问错误之前,请简化您的代码,以便删除所有不必要的变量和常量,只保留导致错误的代码。这样,您就可以经常发现错误,而无需询问。
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