【发布时间】:2023-04-07 09:33:02
【问题描述】:
我正在寻找一种以 JSON 格式返回此语句的方法。
def statement
total = 0
bonus points = 0
result = 'Car rental for #{@name.to_s}\n'
for r in @rentals
this_amount = 0
case r.car.style
when Car::SUV
this_amount += r.days_rented * 30
when Car::HATCHBACK
this_amount += 15
if r.days_rented > 3
this_amount += (r.days_rented - 3) * 15
end
when Car::SALOON
this_amount += 20
if r.days_rented > 2
this_amount += (r.days_rented - 2) * 15
end
else
end
if this_amount < 0
bonus_points -= 10
end
bonus_points = bonus_points + 1
if r.car.style == Car::SUV && r.days_rented > 1
bonus_points = bonus_points + 1
end
result += r.car.title.to_s + "," + this_amount.to_s + "\n"
total += this_amount
end
result += "Amount owed is " + "#{total.to_s}" + "\n"
result +="Earned bonus points: " + bonus_points.to_s
result
end
我需要向我的类添加什么方法才能以 JSON 格式返回此语句?谢谢。
【问题讨论】:
标签: ruby-on-rails json ruby-on-rails-5