开发者

what is the possible reason for require a file but it did not run

what might be the possible reason?

#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 ma开发者_如何学编程in:Object
    from (irb):2
    from :0


The docs for Kernel#load says

In no circumstance will any local variables in the loaded file be propagated to the loading environment.

As piyush says a global or constant is fine.


declare it as constant or global variable. code_words lives inside the scope of require and dies/not-accessible after it has loaded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜