Problem with Why's Poignants guide code..help

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.

Any Python experience by any chance? :slight_smile:

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 G.

Alder G. wrote:

Any Python experience by any chance? :slight_smile:

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 G.

Thanks, but no no Python experience!

Bit of a flaw in the book then at only chapter 4!! If the code does not
work as described.

It seems quite sensible what i’m trying to do, don’t quite understand
your YAML format, but basically how can I store variables, hashes etc in
an external file then call them.

This book has been praised but has anyone really tried the code?

The guide is cute, but I wouldn’t recommend it as an actual tutorial.
Try Chris P.'s tut, or better yet the pickaxe2.

There are a lot of ways to store structured data in Ruby. The most
common way in real world usage is plaintext mark format, like Yaml.
Google Yaml and read about it (somewhat like XML, only usually nicer
but less powerful).

The other common way is Marshal:

http://ruby-doc.org/core/classes/Marshal.html

which makes sense sometimes.

BTW I asked since what you did there kinda, sorta, makes sense in
Python. But not in Ruby.

Alder G. wrote:

The guide is cute, but I wouldn’t recommend it as an actual tutorial.
Try Chris P.'s tut, or better yet the pickaxe2.

There are a lot of ways to store structured data in Ruby. The most
common way in real world usage is plaintext mark format, like Yaml.
Google Yaml and read about it (somewhat like XML, only usually nicer
but less powerful).

The other common way is Marshal:

module Marshal - RDoc Documentation

which makes sense sometimes.

Thanks, consider it canned!

Without looking at anything like YAML etc one question:

I have variables in one file, load file in to another using “require”,
then try to access those variables.

How is this done in standard ruby progs. I know you can do it in Rails
by setting @variable then that is available in the view for example.

James W. wrote:

Bit of a flaw in the book then at only chapter 4!! If the code does not
work as described.

I’m terribly sorry. This has been fixed in the updated book:
http://qa.poignantguide.net/.

We’ve been working so much on the translations and music that I’ve not
had time to update the English site. Soon.

_why

Here’s something that works if you define things in terms of a Ruby
module in the wordlist.rb file:

wordlist.rb

module Wordlist

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

end

And then here’s the other test file:

wordtest.rb

require ‘wordlist’

Get evil idea and swap in code words

print "Enter your new idea: "
idea = gets
Wordlist::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