2009-12-21 9 views
0

Je suis avec 'Why's Poignant Guide to Ruby' afin d'apprendre Ruby et j'ai un problème quand il introduit la méthode 'require' (?) Dans son tutoriel.Problème simple 'require' dans Ruby

Essentiellement je fais un fichier appelé 'wordlist.rb' qui contient:


    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' 
} 

Je puis un autre rubis scipt appelé '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

Lorsque je tente de courir le script Ruby je reçois:

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

Toute idée comment obtenir le « besoin » de travailler correctement à wordlist de référence?

Répondre

3

Il y a un problème avec la variable locale. Vous pouvez regarder here - il y a exactement le même problème avec une solution.