Ruby语法错误,意外的tIVAR,期望输入结束

时间:2015-02-07 19:24:13

标签: ruby syntax-error

Getting this when running program

customerbill.rb:28:语法错误,意外的tIVAR,期望输入结束。我正在尝试用红宝石计算一家餐馆。

class CustomerBill


class Bill < CustomerBill
  def initalize (burgers, drinks, subtotal)
   @burgers  = 6.95 * 5
   @drinks   = 1.75 * 4
   @meal = @burgers + @drinks 
   @totalBill = @meal + @taxAmount + @tipAmount
end

class CustomerTax < CustomerBill
  def initalize (tax, taxAmount, totalWithTax)
  @tax          = 0.0825
  @taxAmount    = @meal * @tax
  @totalWithTax = @meal + @tax
end

结束

class CustomerTip
 def initalize (tipRate, tipAmount)
 @tipRate   = 0.15
 @tipAmount = @totalWithTax * @tipRate
end

puts "Total meal charge #{@meal}"
puts "Tax amount  #{@taxAmount}"
puts "Tip amount  #{@tipAmount}"
puts "Total bill  #{@totalBill}"

2 个答案:

答案 0 :(得分:1)

您缺少关闭定义的结束语句,这就是错误指出“期望输入结束”的原因。使用结束语句来修复所有定义,方法,类等,即

class Bill < CustomerBill

   def initalize (burgers, drinks, subtotal)
      @burgers  = 6.95 * 5
      @drinks   = 1.75 * 4
      @meal = @burgers + @drinks 
      @totalBill = @meal + @taxAmount + @tipAmount
   end

end

答案 1 :(得分:1)

正如在heading_to_tahiti的回答中指出的那样,你有一个缺失的结束语句,但另外你完全误解了ruby中类的使用。你要做的事情就是这样:

burgers  = 6.95 * 5
drinks   = 1.75 * 4
meal = burgers + drinks
tax          = 0.0825
taxAmount    = meal * tax
totalWithTax = meal + taxAmount
tipRate   = 0.15
tipAmount = totalWithTax * tipRate
totalBill = meal + taxAmount + tipAmount



puts "Total meal charge #{meal}"
puts "Tax amount  #{taxAmount}"
puts "Tip amount  #{tipAmount}"
puts "Total bill  #{totalBill}"