tags:

views:

114

answers:

2

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!

A: 

gets.chomp.to_i must be returning nil, so gets.chomp must not be convertible to an integer.

Thom Smith
`String#to_i` never returns `nil`. If the string does not represent an integer, you get `0`.
sepp2k
+3  A: 

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.

mikej
Yes you are correct line 91 points to: guestStayedDays = departureDate - guest.arrived. Your answer confirms then my suspicion. So then the value must be nil I suppose?
Sebastien
Correct. From the error you're seeing it must be `guest.arrived` that is `nil`. `guest` is not `nil` because you are successfully calling `arrived` on it, and `departureDate` is not `nil` because you are successfully calling `-` on that. It's the argument to `-` (`guest.arrived` that is the problem.)
mikej
Thank you mikej for your answers. I will try and see if I can solve this somehow.
Sebastien
I still cant seem to figure out why guest.arrived is nil. Could it have something to do with the fact that the self.check_out method is a class method and that arrived is saved in the self.check_in method?! Might be talking total mambo jambo here but Im a real newbie to Ruby:) I mean i still initilize guest=$camping.current_guests[plot-1] in this method. Help please!
Sebastien
If you email me a copy of the complete program (contact details in [my profile](http://stackoverflow.com/users/63034/mikej)) and the steps to get the error I'll take a quick look. We can then edit the question to add the pertinent details in case they're of interest to anyone else in the future.
mikej
Wow! Thanks mikej. I have emailed you at your .org email. Really nice of you!
Sebastien
This answer helped too. Thanks mikej.
allesklar