我試圖通過使用推向對象添加到數組。我有一個程序,你可以註冊的客人。當我從我的菜單中選擇簽入選項時我無法將新的訪客添加到訪客歷史記錄。未定義的方法'推'爲零: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
沒有收到錯誤信息?感謝所有幫助!
一旦第一個問題得到解答,不要修改您的問題以將其更改爲完全不同的問題。我要編輯這個來恢復你的改變。問第二個問題。 (如果你擔心你會在StackOverflow上問太多問題 - 我們可能不會爲你調試整個程序 - 找一個知道誰可以親自問你的人。) – 2010-08-04 13:36:25
我真的抱歉。這是我第一次實際使用StackOverflow來處理這些事情,所以我對此很陌生。所以這與我害怕提出一個新問題的事實毫無關係,因爲我認爲我可以這樣做。 – Sebastien 2010-08-04 14:26:50