我试图通过使用推向对象添加到数组。我有一个程序,你可以注册的客人。当我从我的菜单中选择签入选项时我无法将新的访客添加到访客历史记录。未定义的方法'推'为零: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