Ruby中的简单“需求"问题 [英] Simple 'require' problem in Ruby
问题描述
为了学习Ruby,我正在跟着《为什么要对Ruby进行艰苦的指导》,而当他在本教程中首次引入'require'方法(?)时,我遇到了一些问题.
I'm following along with 'Why's Poignant Guide to Ruby' in order to learn Ruby and I am having some problem when he first introduces the 'require' method(?) in his tutorial.
基本上,我制作了一个名为'wordlist.rb'的文件,其中包含:
Essentially I make a file called 'wordlist.rb' which contains:
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"的红宝石密码:
I then have another ruby scipt called '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脚本时,我得到:
When I try to run the Ruby script i get:
files.rb:9: undefined local variable or method `code_words' for main:Object (NameError)
是否知道如何使要求"正常工作以引用单词表?
Any idea how to get the 'require' to work properly to reference wordlist?
推荐答案
局部变量存在问题.您可以查看此处-解决方案存在完全相同的问题.
There is a problem with local variable. You can look here - there is exactly the same problem with a solution.
这篇关于Ruby中的简单“需求"问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!