【问题标题】:Simple 'require' problem in RubyRuby 中的简单“要求”问题
【发布时间】:2010-12-28 22:36:18
【问题描述】:

为了学习 Ruby,我正在跟随“Why's Poignant Guide to 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 

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

files.rb:9: main:Object (NameError) 的未定义局部变量或方法“code_words”

知道如何让“要求”正常工作以引用词表吗?

【问题讨论】:

    标签: ruby require


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-25
      • 2011-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多