2009-12-21 51 views
0

为了学习Ruby,我跟着'Why's Poignant Guide to Ruby'进行了学习,当他在他的教程中首次引入'require'方法(?)时,我遇到了一些问题。Ruby中的简单'需求'问题

基本上我做了一个名为 '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

当我尝试运行我得到的Ruby脚本:

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

任何想法如何让'require'正常工作以引用wordlist?

回答

3

局部变量有问题。您可以看看here - 解决方案的问题完全相同。