From: Alder Green Date: 2006-03-21T23:43:30+09:00 Subject: Re: Problem with Why's Poignants guide code..help Any Python experience by any chance? :) In wordlist.rb you create a *local variable* code_words. When you require wordlist.rb, locals variable get created, then destroyed. Only constants (classes, modules) survive. So you can either store that hash as a constant (inside a module/class), or (cleaner, more sensible) store it in some sort of serial representation format, e.g. Yaml. Alder Green On 3/21/06, James Whittaker wrote: > Trying to follow the code on chapter 4 ( > http://poignantguide.net/ruby/chapter-4.html ) and I cannot get the > section called Making the swap'c code to work. > > this is saved as 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' > } > > and this as wordtest.rb: > > 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 > > When I run this from the command line (on WinXP) I enter: > > ruby wordtest.rb > > The program runs asks for an input then blows up saying that undefined > local variable code_words on line 7 in wordtest.rb. > > I have followed everything and the only way I can get a result it to > place the whole code_words has directly into wordtest.rb. So I think it > has something to do with the require statement but still cannot get > anything to work. > > Thanks for your help. > > -- > Posted via http://www.ruby-forum.com/. > >