`-': nil 不能被强制转换为 Fixnum (TypeError) [英] `-': nil can't be coerced into Fixnum (TypeError)

查看:56
本文介绍了`-': nil 不能被强制转换为 Fixnum (TypeError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我收到一个错误,内容为 ./ruby_camping.rb:91:in `-': nil can't be coerced into Fixnum (TypeError).我想做的是结帐我刚入住的客人.这是结帐部分的代码块:

def self.check_out输入欢迎结账!"把 $camping.current_guestsputs 这是当前的客人,你想结账谁?!"输入州地块编号"情节=gets.chomp.to_i来宾 = $camping.current_guests[plot-1]输入说明您的结帐日期(比+guest.arrived.to_s+"大的数字):# 客人在办理入住手续之前不应该结账出发日期 = gets.chomp.to_iguestStayedDays = 出发日期 - guest.arrived #客人入住的天数而 (guestStayedDays <1) 做输入结帐日期无效.客人已入住" + guest.arrived.to_s输入请说明一个新日期."出发日期 = gets.chomp.to_iguestStayedDays = 出发日期 - guest.arrived结尾guest.departure = 出发日期guest.plot.increase(guestStayedDays) # 增加电表puts guest # 打印来宾$camping.current_guests[plot-1] = nil # 清空情节结尾结尾

为什么离开日期仍然为零?感谢帮助!

解决方案

要获得更详细的答案,您必须指明哪一行是程序中的第 91 行.然而,为了给你指明正确的方向,如果你看到 nil can't be coerced into Fixnum 那么它意味着在 右手边-nil.例如

irb(main):001:0>5 - 无类型错误:nil 不能被强制转换为 Fixnum来自 (irb):1:in `-'来自 (irb):1

从您的代码中,我可以看到的唯一可能示例是 guestStayedDays = 出发日期 - guest.arrived,因此我会检查 guest.arrived 的值.

如果 -左侧nil,例如departureDateplot 那么你会得到 undefined method '-' for nil:NilClass .

In my program Im getting an error which says ./ruby_camping.rb:91:in `-': nil can't be coerced into Fixnum (TypeError). What I would like to do is to checkout a guest that I have just checked in. Here is the block of code for the checkout part:

def self.check_out 
  puts "Welcome to checkout!"
  puts $camping.current_guests
  puts " Here are the current guests, who do you want to checkout?!" 
  puts "State plot number "
  plot = gets.chomp.to_i
  guest = $camping.current_guests[plot-1] 
  puts "State the date for your checkout (a number bigger then " + guest.arrived.to_s + "): "
  # it should not be possible for a guest to checkout before she has checked in
  departureDate = gets.chomp.to_i
  guestStayedDays = departureDate - guest.arrived #Days the guest has stayed
  while (guestStayedDays < 1) do                        
   puts "The date for checkout is not valid. The guest checked in " + guest.arrived.to_s
   puts "Please state a new date."
   departureDate = gets.chomp.to_i
   guestStayedDays = departureDate - guest.arrived
  end       
  guest.departure = departureDate
  guest.plot.increase(guestStayedDays) # increases the electricity meter
  puts guest # prints the guest
  $camping.current_guests[plot-1] = nil # emptys the plot
 end
end

How come departureDate still is nil? Thankful for help!

解决方案

To get a more detailed answer, you would have to indicate which line was line 91 in your program. However to point you in the right direction, if you're seeing nil can't be coerced into Fixnum then it means something on the right hand side of a - is nil. e.g.

irb(main):001:0> 5 - nil
TypeError: nil can't be coerced into Fixnum
        from (irb):1:in `-'
        from (irb):1

From your code the only possible example of this I can see is guestStayedDays = departureDate - guest.arrived so I would check the value of guest.arrived.

If something on the left hand side of a - were nil e.g. departureDate or plot then you would get undefined method '-' for nil:NilClass instead.

这篇关于`-': nil 不能被强制转换为 Fixnum (TypeError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆