0
對不起,如果這是一個總的新手問題,但我不知道如何解決這個問題。目前我得到這些錯誤,當我嘗試運行下面的代碼:在嘗試使用Beginning Ruby書籍的第12章中的某些代碼時,如何擺脫此RunTimeError?
bot.rb:58:in `rescue in initialize': Can't load bot data (RunTimeError)
bot.rb:55:in `initialize'
basic_client.rb:3:in `new'
basic_client.rb:3:in `<top (required)>'
下面是bot.rb的源代碼,錯誤似乎是在 「@data = YAML.load( File.open(options [:data_file])。read)「部分。
# A basic implementation of a chatterbot
class Bot
attr_reader :name
# Initializes the bot object, loads in the external YAML data
# file and sets the bot's name. Raises an exception if
# the data loading process fails.
def initialize(options)
@name = options[:name] || "Unnamed Bot"
begin
@data = YAML.load(File.open(options[:data_file]).read)
rescue
raise "Can't load bot data"
end
end
這是basic_client.rb文件的源代碼:
require './bot'
bot = Bot.new(:name => ARGV[0], :data_file => ARGV[1])
puts bot.greeting
while input = $stdin.gets and input.chomp != 'end'
puts '>> ' + bot.response_to(input)
end
puts bot.farewell
如果有人可以幫助我,會是巨大的。此外,如果您需要更多信息或澄清問題,我也可以提供。
謝謝!
你是否需要'yaml''?除了@ mu的建議,您可能會更改'raise「無法將bot數據加載到'raise」無法加載來自'#{options [:data_file]}'''的bot數據。 –
雅我做了要求'yaml',但我想也許我沒有正確引用另一個文件,或者有一些與我試圖運行的文件相關的其他文件有關。 – Jamaal