Problem with Why's Poignants guide code..help

J

James Whittaker

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::eek:pen( "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.
 
A

Alder Green

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
 
J

James Whittaker

Alder said:
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

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

Alder Green

The guide is cute, but I wouldn't recommend it as an actual tutorial.
Try Chris Pine'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.
 
J

James Whittaker

Alder said:
The guide is cute, but I wouldn't recommend it as an actual tutorial.
Try Chris Pine'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.

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

Alder Green

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

The guide is cute, but I wouldn't recommend it as an actual tutorial.
Try Chris Pine'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.
 
G

gregarican

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::eek:pen( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
 
W

why the lucky stiff

James said:
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
 
G

gregarican

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::eek:pen( "idea-" + idea_name + ".txt", "w" ) do |f|
f << idea
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top