2013-10-19 39 views
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 

如果有人可以幫助我,會是巨大的。此外,如果您需要更多信息或澄清問題,我也可以提供。

謝謝!

+1

你是否需要'yaml''?除了@ mu的建議,您可能會更改'raise「無法將bot數據加載到'raise」無法加載來自'#{options [:data_file]}'''的bot數據。 –

+0

雅我做了要求'yaml',但我想也許我沒有正確引用另一個文件,或者有一些與我試圖運行的文件相關的其他文件有關。 – Jamaal

回答

0

更改救援,這樣就可以看到完整的消息:

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

然後我想說的是得到一個錯誤出現意味着要麼你的文件的格式不正確(檢查與http://yamllint.com/語法)或路徑不正確。你需要確保你正確地傳遞你的YAML文件的路徑basic_client.rb作爲第二個參數(argv [1]):

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

我不知道如何「bot.yml」應該看像,但它必須是您期望在您的@data變量中的數據。

相關問題