Nel mio programma Im ottenendo un errore che dice ./ruby_camping.rb:91:in` -': nil non può essere forzato in Fixnum (TypeError). Quello che vorrei fare è alla cassa un ospite che ho appena controllato nella Ecco il blocco di codice per la parte checkout:.`- ': nil non può essere forzato in 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
Come mai departureDate ancora è pari a zero? Grazie per l'aiuto!
Sì, si è linea corretta 91 punti a: guestStayedDays = departureDate - guest.arrived. La tua risposta conferma allora il mio sospetto. Quindi il valore deve essere nullo, suppongo? – Sebastien
corretto. Dall'errore che stai vedendo deve essere 'guest.arrived' che è' nil'. 'guest' non è' nil' perché stai chiamando con successo 'arrived' su di esso, e' departureDate' non è 'nil' perché stai chiamando' -' con successo. È l'argomento di '-' (' guest.arrived' è il problema.) – mikej
Grazie a mikej per le tue risposte. Proverò a vedere se riesco a risolvere questo in qualche modo. – Sebastien