2010-08-03 103 views
0

我試圖通過使用推向對象添加到數組。我有一個程序,你可以註冊的客人。當我從我的菜單中選擇簽入選項時我無法將新的訪客添加到訪客歷史記錄。未定義的方法'推'爲零:NilClass(NoMethodError)Ruby

這不工作:

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 

在這裏,我得到一個check_in': undefined method推」的零:NilClass(NoMethodError)。但如果我註釋掉這條線:

#@all_guests.push(guest) # adds the guest to the history 

我能夠註冊一個客人。然後當我從我的菜單中選擇3選項時:

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 

然後我在那裏看到客人。唯一的問題是,我不能做4選項,這是顯示所有客人,因爲我已經註釋掉了這行代碼。因此,如何使用下面這行代碼:

@all_guests.push(guest) # adds the guest to the history 

沒有收到錯誤信息?感謝所有幫助!

+0

一旦第一個問題得到解答,不要修改您的問題以將其更改爲完全不同的問題。我要編輯這個來恢復你的改變。問第二個問題。 (如果你擔心你會在StackOverflow上問太多問題 - 我們可能不會爲你調試整個程序 - 找一個知道誰可以親自問你的人。) – 2010-08-04 13:36:25

+0

我真的抱歉。這是我第一次實際使用StackOverflow來處理這些事情,所以我對此很陌生。所以這與我害怕提出一個新問題的事實毫無關係,因爲我認爲我可以這樣做。 – Sebastien 2010-08-04 14:26:50

回答

4

@all_guests沒有在您得到錯誤的文件中定義,它在$camping全局中定義。將代碼更改爲

$camping.all_guests.push(...) 

並查看該功能是否適用於您。

+1

該死!謝謝!!令人難以置信的是我一直與那個人一起掙扎多久。我欠你:) – Sebastien 2010-08-03 20:01:10

+0

不客氣。如果這解決了你的問題,你介意接受答案嗎? – 2010-08-04 14:00:44

+0

它的確如此,謝謝!我當然可以接受它! – Sebastien 2010-08-04 14:19:50

相關問題