在从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 `' 

这是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 

如果有人能帮助我,那就太好了。 此外,如果您需要有关问题的更多信息或说明,我也可以提供。

谢谢!

更改救援,以便您可以看到完整的消息:

  rescue => e raise "Can't load bot data because: #{e}" 

然后我会说在那里收到错误意味着您的文件格式不正确(请使用http://yamllint.com/检查其语法)或路径不正确。 您需要确保正确地将yaml文件的路径作为第二个参数(ARGV [1])传递给basic_client.rb:

 ruby basic_client.rb "C3PO" "bot.yml" 

我不确定“bot.yml”应该是什么样子,但它必须是你期望在@data变量中的数据。