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.
精彩评论