Простая задача 'require' в Ruby - PullRequest
0 голосов
/ 22 декабря 2009

Я изучаю Ruby, следуя инструкциям «Зачем нужен Poignant Guide to 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'
}

Затем у меня есть другой рубиновый scipt, называемый files.rb:

<code>
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 

Когда я пытаюсь запустить скрипт Ruby, я получаю:

files.rb:9: undefined local variable or method `code_words' for main:Object (NameError)
</code>

Есть идеи, как заставить 'require' работать правильно для ссылки на список слов?

1 Ответ

3 голосов
/ 22 декабря 2009

Проблема с локальной переменной. Вы можете посмотреть здесь - точно такая же проблема с решением.

...