救命! check_in':未定义方法`push'代表nil:NilClass(NoMethodError)

时间:2010-08-02 13:45:51

标签: ruby

嗨我得到一个核心错误,这是我在Ruby中的标准,但不知道该怎么做。我有一个我写过的程序。其目的是在露营地登记客人。你有一个菜单有5种不同的选择。 1.签到。当我这样做时,我得到一个未定义的方法generateParkingLot' for #<Camping:0x10030a768> (NoMethodError) When I choose Checkout I get a undefined local variable or method出发'为Menu:Class(NameError)。 所以,请有人知道我的问题,这将是伟大的。我将在这里粘贴我的所有代码。代码在不同的文件中分开,我使用了不同文件的require。虽然我将所有代码粘贴到一个跟踪中。感谢所有的帮助。

-Sebastien

require 'guest'

require 'parking_lot' 

class Camping 

attr_accessor :current_guests, :parking_lots, :all_guests, :staticGuests

def initialize(current_guests, parking_lots, all_guests, staticGuests)

   @current_guests = Array.new()

 # initiera husvagnsplatserna
 @parking_lots = Array.new(32)
 32.times do |nr|
     @parking_lots[nr] = Parking_Lot.new(nr)

    @staticGuests = Array[ 
    Guest.new("Logan Howlett", "Tokyo", "07484822",1, @parking_lots[0]), 
    Guest.new("Scott Summers", "Chicago", "8908332", 2, @parking_lots[1]),
        Guest.new("Hank Moody", "Boston", "908490590", 3, @parking_lots[2]),
        Guest.new("Jean Grey", "Detroit", "48058221", 4, @parking_lots[3]),
        Guest.new("Charles Xavier","Washington DC", "019204822",5, @parking_lots[4])
            ] 

   end 

@all_guests = []    

  @staticGuests.each do |guest|
  @current_guests[guest.plot.nr] = guest
  @all_guests.push(guest)
 end                            
end


def to_s
    # creates an empty string 
    list = " "

    # loop from 1 to 32
    (1..32).each do |n|
        if (!@current_guests[n-1].nil?)
            list += @current_guests[n-1].to_s
        else
            # else adds the text "Vacant"
            list += n.to_s + ": Vacant!\n"
        end
    return list
end

def generateParkingLot

    randomNr = 1+rand(32)
    # exists a guest at the (0-based) position?
    if (!@current_guests[randomNr-1].nil?)
        # if so generate a new figure
      generateEmpty(array)
    else
        # returns the generated number
        return randomNr
    end
    end
     end
  end

class Guest

attr_accessor :firstname, :lastname, :address, :phone, :departure
attr_reader :arrived, :plot


def initialize (firstName, lastName, address, phone, plot)  
 @firstName = firstName
 @lastName = lastName
 @address = address
 @phone = phone
 @arrived = arrived
 @plot = plot           

  def to_s 
    "Personal information:
     (#{@firstName}, #{@lastName}, #{@address}, #{@phone}, #{@arrived}, #{@departure},      #{@plot})" 
  end 
end                     


require 'ruby_camping'
require 'camping_guests'

class Main  

if __FILE__ == $0   
  $camping = Camping.new(@current_guests, @all_guests, @parking_lots,@staticGuests)
  puts "\n"
  puts "Welcome to Ruby Camping!"

 while (true)
   Menu.menu
  end
 end   
end 


require 'date'                     
require 'camping_guests' 
require 'guest'

class Menu

def initialize(guests = [])
    @camping = Camping.new(guests)
end



def self.menu  
  puts "---------------------------"
  puts "      Menu"                          
  puts "  1. Checkin"
  puts "  2. Checkout"
  puts "  3. List current guests"
  puts "  4. List all guests"
  puts "  5. Exit\n"
  puts ""
  puts " What do you want to do?"
  puts "---------------------------"
  print ": "
  action = get_input
    do_action(action)
end

# fetches menu choice and returns chosen alternativ 
def self.get_input  
  input = gets.chomp.to_i

 while (input > 5 || input < 1) do
     puts "Ooups, please try again."
     input = gets.chomp.to_i
 end 
  return input
end

def self.do_action(action)
  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


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
    newGuest = Guest.new(firstName, lastName, address,    phone,arrived,$camping.parking_lots[newPLot-1])
    $camping.current_guests[newPLot-1] = newGuest
    @all_guests.push(newGuest) 
    puts "The registration was a success!! You have received the  " + newPLot.to_s + "."
  end                          

  def self.check_out 
    puts "Welcome to checkout!"
    puts $camping.all_guests
    puts "State plot of the person to checkout!"
    plot = gets.chomp.to_i
    puts "Ange utcheckningsdatum: "
    departureDate = gets.chomp.to_i
    guest = $camping.current_guests[plot-1]      
    @departure = departure   
    guest.departure = departureDate
    guestStayedDays = departureDate - guest.arrived      
    guest.plot.increase(guestStayedDays)                
    puts guest                                      
    $camping.current_guests[plot-1] = nil               
  end
 end


class Parking_Lot

attr_accessor :nr
attr_reader :electricity_meter

def initialize (nr)
    @nr = nr
    @electricity_meter = 4000-rand(2000)    
    end

def increase_meter(days)
    generatedUse = (10+rand(70))*days
    puts "Increases the meter with " + generatedUse.to_s + " kWh."
    @electricity_meter += generatedUse
end

def to_s

  "Plot #{@nr+1} Electricity meter: #{@electricity_meter} kWh"

end 
 end 

1 个答案:

答案 0 :(得分:1)

看起来(虽然我没有试过这个),就像你的一些'你的结局是错的。

造成第一个错误的那个(generateParkingLot undefined)是generateParkingLot实际上是在里面 to_s中定义的,所以你需要在你的to_s方法结束时额外加上'end'。

至于第二个错误(离开未识别),self.check_out中的下一行有错:

@departure = departure

因为没有“离开”变量。 (也许你的意思是DepartureDate?)。我怀疑这段代码可能还有其他一些问题,但我恐怕现在没时间检查。

我注意到的是,当你有

32.times do |nr|
     @parking_lots[nr] = Parking_Lot.new(nr)

我想你可能希望以“结束”或大括号结束,例如。

32.times do |nr|
     @parking_lots[nr] = Parking_Lot.new(nr)
end

虽然这会使你的其他块不匹配..一般情况下,只需尝试确保你的块都已正确定义(例如,所有块都有匹配的结束)。