Ruby ';要求';提出一个命名错误
我已将以下哈希保存在file2.rb下Ruby ';要求';提出一个命名错误,ruby,debugging,require,Ruby,Debugging,Require,我已将以下哈希保存在file2.rb下 codewords = {'starmonkeys' => 'Five edged primates.'} *你们中的一些人可能会从辛酸的指南中认识到这一点 以及file1.rb下的以下内容: if __FILE__ == $0 require File.expand_path('C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\why the lucky stiff made me
codewords = {'starmonkeys' => 'Five edged primates.'}
*你们中的一些人可能会从辛酸的指南中认识到这一点
以及file1.rb下的以下内容:
if __FILE__ == $0
require File.expand_path('C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\why the lucky stiff made me do this\file2.rb', __FILE__)
print "Enter your secret sentence."
idea = gets
codewords.each do | real , code|
idea.gsub!( real , code )
end
#Saving to a new file
print "File encoded. Print a file name for this idea"
idea_name = gets.strip
File::open( "idea-"+idea_name+".txt","w" ) do |f|
f<<idea
end
end
codewords.each do | real , code|
idea.gsub!( real , code )
end
如果文件=0美元
require File.expand_path('C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\why the lucky stiff让我这么做\file2.rb',uuuu File_uuu)
打印“输入你的秘密句子。”
想法=得到
码字。每一个都是真实的,代码|
idea.gsub!(实数,代码)
结束
#保存到新文件
打印“文件编码。打印此想法的文件名”
idea\u name=get.strip
文件::open(“idea-”+idea_name+”.txt、“w”)do | f|
据我所知,变量声明的作用域是定义它们的文件。这意味着它们基本上不存在于文件范围之外。为了弥补这一差距,您可能需要使用常量、类或模块,或者出于名称空间的目的使用它们的组合。例如:
# file2.rb
module Codewords
CODEWORDS = { ... }
end
以后,您可以这样要求和使用:
include Codewords
CODEWORDS.each do |real, code|
# ...
end
您可以将码字声明为全局的,然后稍后通过将其引用为
$codewords = {'starmonkeys' => 'Five edged primates.'} #file2.rb
然后在file1.rb中:
if __FILE__ == $0
require File.expand_path('C:\Users\COMPAQ\My Documents\Aptana Studio Workspace\why the lucky stiff made me do this\file2.rb', __FILE__)
print "Enter your secret sentence."
idea = gets
codewords.each do | real , code|
idea.gsub!( real , code )
end
#Saving to a new file
print "File encoded. Print a file name for this idea"
idea_name = gets.strip
File::open( "idea-"+idea_name+".txt","w" ) do |f|
f<<idea
end
end
codewords.each do | real , code|
idea.gsub!( real , code )
end
嗯,谢谢……你能不能在这个页面的副标题下向下滚动到一半多一点,上面写着“你的代表性是值得的”,告诉我为什么作者声称它在那里有效?或者我不能在更新的Ruby版本中使用这样的require吗?谢谢。@HareKrishna:你不是第一个遇到这个问题的人:zeteic,这个链接真的很有帮助。但事实证明,找到《辛酸指南》的固定副本很有挑战性,因为很多镜子都坏了。如果你知道的话,请告诉我。话虽如此,这解决了我的问题,还有一点更多!