2010-08-03 106 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

相关问题