简体   繁体   English

未定义的方法'push'for nil:NilClass(NoMethodError)Ruby

[英]undefined method `push' for nil:NilClass (NoMethodError) Ruby

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 method push' for nil:NilClass (NoMethodError). 在这里,我得到了一个check_in': undefined method nil:NilClass(NoMethodError)的check_in': undefined method push'。 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: 然后,当我从菜单中选择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

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. 唯一的问题是我无法显示4位客人,因为我已注释掉那行代码。 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! 感谢所有的帮助!

@all_guests isn't defined in the file where you get the error, it's defined in the $camping global. @all_guests没有在您得到错误的文件中定义,而是在$camping全局文件中定义。 Change the code to 将代码更改为

$camping.all_guests.push(...)

and see if that works better for you. 看看是否更适合您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby的nil:NilClass未定义方法“ pair_push”(NoMethodError) - Ruby undefined method `pair_push' for nil:NilClass (NoMethodError) 在 ruby 中获取“nil:NilClass (NoMethodError) 的未定义方法‘push’” - Getting “undefined method `push' for nil:NilClass (NoMethodError)” in ruby Ruby未定义方法`[]&#39;表示nil:NilClass(NoMethodError)错误 - Ruby undefined method `[]' for nil:NilClass (NoMethodError) error Ruby:NoMethodError:nil:NilClass的未定义方法“ +” - Ruby: NoMethodError: undefined method `+' for nil:NilClass Ruby块-NoMethodError:nil:NilClass的未定义方法`-&#39; - Ruby block - NoMethodError: undefined method `-' for nil:NilClass ruby NoMethodError未定义的方法&#39;balance&#39;为nil:NilClass - ruby NoMethodError undefined method `balance' for nil:NilClass Ruby On Rails - NoMethodError:nil:NilClass 的未定义方法“[]” - Ruby On Rails - NoMethodError: undefined method `[]' for nil:NilClass Ruby:nil的未定义方法&#39;length&#39;:NilClass(NoMethodError) - Ruby: Undefined method 'length' for nil:NilClass (NoMethodError) Ruby:NoMethodError:nil的未定义方法`&lt;&lt;&#39;:NilClass - Ruby: NoMethodError: undefined method `<<' for nil:NilClass undefined方法[]为nil:ruby中的NilClass(NoMethodError)...为什么? - undefined method [] for nil:NilClass (NoMethodError) in ruby… Why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM