2011-01-13 59 views
0

可能是什么可能的原因?什么是可能的原因需要一个文件,但它并没有跑

#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' 
} 


irb(main):001:0> require 'wordlist.rb' 
=> true 
irb(main):002:0> code_words 
NameError: undefined local variable or method `code_words' for main:Object 
    from (irb):2 
    from :0 

回答

3

该文档为Kernel#load

在任何情况下将加载文件中的任何地方 变量是 传播到负载的环境。

正如piyush所说的,一个全局或常量是好的。

+0

但是这段代码在里面很辛苦......嗯,他们错了? – wizztjh 2011-01-14 03:27:41

2

将其声明为常量或全局变量。 code_words存在于require的范围内,并且在加载之后死亡/不可访问。

+1

或者一个实例变量,例如`@code_words = ...` – Phrogz 2011-01-13 15:07:58

相关问题