From: "Brian Schröder" Date: 2005-10-19T19:11:46+09:00 Subject: Re: require problem On 19/10/05, eoghan wrote: > Brian Schr�der wrote: > > On 19/10/05, eoghan wrote: > >> Hello > >> Im doing a simple test. I have 2 files: > >> mouse.rb > >> --------- > >> my_string = 'blah' > >> > >> rabbit.rb > >> --------- > >> require 'foo' > >> > >> print my_string > >> > >> eoghanj$ /opt/local/bin/ruby bar.rb > >> bar.rb:3: undefined local variable or method `my_string' for > >> main:Object (NameError) > >> > >> Im simplified my example down to this; and I cant see what im doing > >> wrong... hope its not too stupid. > >> I read this part about irb restrictions, but im not sure it applies? > >> http://www.rubycentral.com/book/irb.html > >> Thanks > >> Eoghan > >> > >> > > > > Unlike in php or c require and load do not simply replace the > > statement with the file, but are carefull not to introduce any local > > variables. Try the following. > > > > mouse.rb > > --- > > module AnimalConstants > > MOUSE_NAME = "Mickey Mouse" > > end > > > > rabbit.rb > > ---s > > require 'mouse' > > > > p AnimalConstants::MOUSE_NAME > > > > Constants and Globals are imported. (Note that you do not need to > > structure your constants using a module, it was just to show this > > effekt.) > > > > regards, > > > > Brian > > Thanks I understand what you are saying... however consider the > following example from whys guide (and maybe im missing something here > again...) > http://www.poignantguide.net/ruby/chapter-4.html#section3 > > 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' > } > =========================================================== > > i assume this should be saved to a file too...? > =========================================================== > require 'wordlist' > > # Get evil idea and swap in code words > print "Enter your new idea: " > idea = gets > code_words.each do |real, code| > 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 << idea > end > > This doesnt work either. For me at least. Any ideas? > Eoghan > > Hello Eoghan, this is a known bug in _why's tutorial. It would be a good idea if it were fixed in the tutorial, as this question comes up from time to time on this mailing list. Change code_words to CODE_WORDS and it works. regards, Brian -- http://ruby.brian-schroeder.de/ Stringed instrument chords: http://chordlist.brian-schroeder.de/