Я слежу за «Why's Poignant Guide to 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:
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)
Есть идеи, как заставить "требовать" работать правильно, чтобы ссылаться на список слов?