gpt4 book ai didi

ruby - 帮助! check_in' : undefined method `push' for nil:NilClass (NoMethodError)

转载 作者:太空宇宙 更新时间:2023-11-03 16:38:46 25 4
gpt4 key购买 nike

您好,我遇到了一个核心错误,我认为这是 Ruby 中的标准错误,但不知道该怎么做。我有一个我写的程序。它的目的是在露营地登记客人。您有一个包含 5 个不同选项的菜单。 1.签到。当我这样做时,我得到一个未定义的方法 generateParkingLot' for #<Camping:0x10030a768> (NoMethodError)
When I choose Checkout I get a undefined local variable or method
菜单:类(NameError)的出发'。所以请我有人知道我的问题,这会很棒。我将在这里粘贴我所有的代码。代码在不同的文件中分开,我对不同的文件使用了 require 。不过在这里,我会将所有代码粘贴在一条线上。感谢所有帮助。

-塞巴斯蒂安

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

最佳答案

看起来(尽管我还没有尝试过)你的某些“结局”是错误的。

导致您的第一个错误(generateParkingLot 未定义)的错误是 generateParkingLot 实际上是在 inside to_s 中定义的,因此您需要在 to_s 方法的末尾添加一个额外的“end”。

至于第二个错误(departure not recognised),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

尽管这会使您的其他 block 不匹配。通常,只需尝试确保您的 block 都已正确定义(例如,所有 block 都有一个匹配的末端)。

关于ruby - 帮助! check_in' : undefined method `push' for nil:NilClass (NoMethodError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3388342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com