tags:

views:

69

answers:

1

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 methodpush' 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!

+1  A: 

@all_guests isn't defined in the file where you get the error, it's defined in the $camping global. Change the code to

$camping.all_guests.push(...)

and see if that works better for you.

Steve Madsen
Damn! Thank you!! Incredible how long Ive been struggling with that one. I owe u:)
Sebastien
You're welcome. If that solved your problem, would you mind accepting the answer?
Steve Madsen
It did, thank you! Of course I can accept it!
Sebastien