Im trying to add objects to an array by using push. I have a program where u can register guests. When I choose the checkin option from my menu Im unable to add the new guest to the guest history.
This dont work:
def self.check_in
puts "Welcome to the checkin"
puts "Please state your first name: "
firstName = gets.chomp
puts "Please state your last name:"
lastName = gets.chomp
puts "Write your address: "
address = gets.chomp
puts "and your phone number: "
phone = gets.chomp
puts "finally, your arrival date!"
arrived = gets.chomp
newPLot = $camping.generateParkingLot
guest = Guest.new(firstName, lastName, address, phone, arrived)
$camping.current_guests[newPLot-1] = guest
puts "The registration was a success!! You have received plot " + newPLot.to_s + "."
@all_guests.push(guest) # adds the guest to the history
end
Here I get a check_in': undefined method
push' for nil:NilClass (NoMethodError). But if I comment out this line:
#@all_guests.push(guest) # adds the guest to the history
Im able to register a guest. An then when I choose the 3 option from my menu which is:
def self.do_action(action)
# utför händelse baserat på valet
case action
when 1:
check_in
when 2:
check_out
when 3:
puts $camping.current_guests
when 4:
puts $camping.all_guests
when 5:
puts "You are now leaving the camping, welcome back!"
exit
end
end
Then I see the guest there. The only problem is that I cant do the 4 option which is to show all guests because I have commented out that line of code. So to round things up how can I use this line of code:
@all_guests.push(guest) # adds the guest to the history
Without receiving the error message?? Thankful for all help!