加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – ` – ‘:nil不能被强制转换为Fixnum(TypeError)

发布时间:2020-12-16 20:48:44 所属栏目:百科 来源:网络整理
导读:在我的程序中我得到一个错误,上面写着./ruby_camping.rb:91:in` – ‘:nil无法强制进入Fixnum(TypeError).我想做的是结账我刚刚签到的客人.以下是结账部分的代码块: def self.check_out puts "Welcome to checkout!" puts $camping.current_guests puts
在我的程序中我得到一个错误,上面写着./ruby_camping.rb:91:in` – ‘:nil无法强制进入Fixnum(TypeError).我想做的是结账我刚刚签到的客人.以下是结账部分的代码块:
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

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

解决方法

要获得更详细的答案,您必须指明程序中第91行是哪一行.然而,为了指出你正确的方向,如果你看到nil不能被强制进入Fixnum那么它意味着一个在右边的东西 – 是零.例如
irb(main):001:0> 5 - nil
TypeError: nil can't be coerced into Fixnum
        from (irb):1:in `-'
        from (irb):1

从你的代码我可以看到的唯一可能的例子是guestStayedDays = departureDate – guest.arrived所以我会检查guest.arrived的值.

如果a – 左侧的某些内容为零,例如离开日期或情节然后你会得到未定义的方法’ – ‘为nil:NilClass而不是.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读