Forum: Ruby Problem with Why's Poignants guide code..help

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-03-21 15:32
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.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-21 15:43
(Received via mailing list)
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
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-03-21 15:47
Alder Green 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 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?
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-21 15:54
(Received via mailing list)
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.
B0be4bfef5a22697a0f44f4fe295aaef?d=identicon&s=25 James Whittaker (aftershock)
on 2006-03-21 16:04
Alder Green wrote:
> 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.
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2006-03-21 16:57
(Received via mailing list)
BTW I asked since what you did there kinda, sorta, makes sense in
Python. But not in Ruby.
33ec7e55a251c1be8d6febfd929aebbe?d=identicon&s=25 Greg Kujawa (gregarican)
on 2006-03-21 18:49
(Received via mailing list)
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
4674615d2cf231975c741731be9a8685?d=identicon&s=25 why the lucky stiff (Guest)
on 2006-03-21 19:32
(Received via mailing list)
James Whittaker 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
This topic is locked and can not be replied to.