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

Ruby syntax error, unexpected tIVAR, expecting end-of-input

Getting this when running program

customerbill.rb:28:语法错误,意外的 tIVAR,预期输入结束。我正在尝试计算 ruby 的餐厅入住率。

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}"

您缺少关闭定义的结束语句,这就是错误状态 "expecting end-of-input" 的原因。用结束语句修复关闭所有定义、方法、类 等,即

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

正如 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}"