PDA

View Full Version : Require method and the errors it brings along


Calixto
October 19th, 2006, 04:37 PM
So, I've recently started using Ruby (in effect making me a 'newb') and I've been learning it form the book known as 'Why the stiff's poignant guide to ruby'.

Now, with that out of the way, I like to work along with the examples given in the book, and I've reached one that just doesn't want to work. If you've read the book, you might remember, bu here it is:

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




The above hash is saved in a file called 'wordlist.rb'. Followed by:

require 'wordlist'


# Get new evil idea and encode it
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end

# Save the new jibberish into 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


From what I understand it, the require method is supposed to go get the wordlist file to use in the new, er, program thingy. However, once it reaches to the part where it says 'code_words.each' it trips up saying that 'code_words' is undefined.


I'm very confused, if anyone could help out a beginner, I would be very grateful.


-Calixto

motobass
October 21st, 2006, 02:25 AM
That appears to be an error in the Guide -- http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152758. That link points you to a newer version of the Guide. I guess it is beta or something like that. Nonetheless, the same issue seems to exist there too. You can get around the "undefined local variable" by making code_words global with "$" on "code_words" in both files like so $code_words.