私はRubyを学ぶために「なぜRubyの痛烈なガイド」をフォローしていますが、彼がチュートリアルで「require」メソッド(?)を最初に紹介したときに問題が発生しています。
基本的に、次の内容を含む「wordlist.rb」というファイルを作成します。
code_words = {
'starmonkeys' => 'Phil and Pete, those prickly chancellors of the New Reich',
'catapult' => 'chucky go-go', 'firebomb' => 'Heat-Assisted Living',
'Nigeria' => "Ny and Jerry's Dry Cleaning (with Donuts)",
'Put the kabosh on' => 'Put the cable box on'
}
次に、「files.rb」という別のルビーのsciptがあります。
require 'wordlist'
#Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
#real will have the key and code will have the value
#Method followed by ! (like gsub!) are known as destructive methods
code_words.each do |real, code|
safe_idea = idea.gsub!( real,code )
end
#Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::open( "idea-" + idea_name + ".txt", "w") do |f|
f
When I try to run the Ruby script i get:
files.rb:9: undefined local variable or method `code_words' for main:Object (NameError)
ワードリストを参照するために「require」を正しく機能させる方法はありますか?