# Problem with Why's Poignants guide code..help

**URL:** <https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477>\
**Category:** Ruby\
**Created:** [March 21, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477 "2006-03-21T15:32:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/1 "2006-03-21T15:32:47Z")

</div>

Trying to follow the code on chapter 4 (  
[http://poignantguide.net/ruby/chapter-4.html](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.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 3:43pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/2 "2006-03-21T15:43:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 3:47pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/3 "2006-03-21T15:47:43Z")

</div>

Alder G. wrote:

> 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 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?

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 3:54pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/4 "2006-03-21T15:54:24Z")

</div>

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](http://ruby-doc.org/core/classes/Marshal.html)

which makes sense sometimes.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 4:57pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/5 "2006-03-21T16:57:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/6 "2006-03-21T16:04:09Z")

</div>

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](http://ruby-doc.org/core/classes/Marshal.html)
> 
> 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.

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 7:32pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/7 "2006-03-21T19:32:26Z")

</div>

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/](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

---

<div class="post-metadata">

**Author:** ![aftershock](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/8e7dd6/32.png) [@aftershock](https://www.ruby-forum.com/u/aftershock)\
**Post date:** [March 21, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/problem-with-whys-poignants-guide-code-help/55477/8 "2006-03-21T18:49:00Z")

</div>

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
