在 Ruby 中工作,我收到一条错误消息
'add': undefined local variable or method 'food' for #<FoodDB:...
这是我要运行的代码
require_relative 'FoodDB.rb'
class Manager
def initialize
food = FoodDB.new
self.create_foodDB(food)
end
def create_foodDB(food)
counter = 1
word = []
file = File.new("FoodDB.txt","r")
while (line = file.gets)
food.addFood(line)
counter = counter + 1
end
file.close
end
end
manager = Manager.new
input_stream = $stdin
input_stream.each_line do |line|
line = line.chomp
if line == "quit"
input_stream.close
end
end
这是 FoodDB.rb 的代码
class FoodDB
def initialize
food = []
end
def addFood(str)
food.push(str)
end
end
我不确定是什么问题,因为我似乎肯定从 FoodDB 类中调用了正确的方法。感谢所有帮助,谢谢!