[英]Simple 'require' problem in Ruby
為了學習Ruby,我正在跟着《為什么要對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'
}
然后,我有另一個名為“ files.rb”的Ruby Scipt:
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
When I try to run the Ruby script i get:
files.rb:9: undefined local variable or method `code_words' for main:Object (NameError)
任何想法如何使“要求”正常工作以引用單詞表?
局部變量有問題。 您可以在這里查看 -解決方案存在完全相同的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.