Getting "Require" to work, using the Poignant Guide to Ruby

In a (doomed?) attempt to actual understand ruby (with my almost 20
years of coding experience), I'm working my way thur "Why's
(Poignant) Guide to Ruby", chapter 4 "Floating Little Leaves of Code"
and doing the "Making the Swap" section example. (on a Win XP box)

and although the "require" loads the wordlist.rb file to load using the
require 'wordlist' method, I was getting "undefined local variable or
method `code_words' for main:Object (NameError)" WTF?!!

the wordlist.rb was :

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, to make it an even simpler example, the calling file was

# SimReq.rb to test require loading
require 'wordlist'
code_words.each {|key, value| print "Key:", key, " for Value:", value,
"\n" }

Just get the require file and do something with the the variable
"Code_words".

I got around the "undefined local variable" by making code_words global
with "$" on "Code_words" in both files. (i.e. "$code_words")

So was it a scoping problem?

Is there a better, or best practice, to do something like this trivial
example or is this Chunky Bacon payback for actually doing the
examples? Or someOther PEBKAC issue?

Please advise O' Great and Powerful OZ!..I mean comp.lang.ruby....

Ian

Hi --

In a (doomed?) attempt to actual understand ruby (with my almost 20
years of coding experience), I'm working my way thur "Why's
(Poignant) Guide to Ruby", chapter 4 "Floating Little Leaves of Code"
and doing the "Making the Swap" section example. (on a Win XP box)

and although the "require" loads the wordlist.rb file to load using the
require 'wordlist' method, I was getting "undefined local variable or
method `code_words' for main:Object (NameError)" WTF?!!

Local variables used in a file you 'require' do not appear in the
requirer's scope. Usually the best thing is to wrap what you need in
a class or module:

   class CodeWords
     def initialize
       { 'a' => 'b',
         'c' => 'd' }
     end
   end

then in the requiring file:

   require 'codewords'
   code_words = CodeWords.new

or something like that. (Maybe use a constant instead of a method.)

David

···

On Sun, 11 Sep 2005, Ian FalsePositives wrote:

--
David A. Black
dblack@wobblini.net

This is an error in the guide, see

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152758

···

On Sun, 11 Sep 2005 04:26:34 +0200, Ian FalsePositives <Ian.Irving@gmail.com> wrote:

In a (doomed?) attempt to actual understand ruby (with my almost 20
years of coding experience), I'm working my way thur "Why's
(Poignant) Guide to Ruby", chapter 4 "Floating Little Leaves of Code"
and doing the "Making the Swap" section example. (on a Win XP box)

and although the "require" loads the wordlist.rb file to load using the
require 'wordlist' method, I was getting "undefined local variable or
method `code_words' for main:Object (NameError)" WTF?!!

Many Thanks for the replys. Problem resolved! by

a) using the fixed version of the Poignant Guide @
http://qa.poignantguide.net/, not the usual url location. (and why
isn't the version @ http://poignantguide.net/ruby/ upto date or
redirecting? )

c) Using a Costant is a better practices and in this case the simplest
thing that works.

d) Make a Class is a best pratice esp if your doing something more than
trivial

Ian www.FalsePositives.com

2005/9/11, Dominik Bathon <dbatml@gmx.de>:

> In a (doomed?) attempt to actual understand ruby (with my almost 20
> years of coding experience), I'm working my way thur "Why's
> (Poignant) Guide to Ruby", chapter 4 "Floating Little Leaves of Code"
> and doing the "Making the Swap" section example. (on a Win XP box)
>
> and although the "require" loads the wordlist.rb file to load using the
> require 'wordlist' method, I was getting "undefined local variable or
> method `code_words' for main:Object (NameError)" WTF?!!

This is an error in the guide, see

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152758

it's just an example, the file you're trying to load doesn't actually
exist.

···

On Sun, 11 Sep 2005 04:26:34 +0200, Ian FalsePositives > <Ian.Irving@gmail.com> wrote:

Ian FalsePositives wrote:

a) using the fixed version of the Poignant Guide @
http://qa.poignantguide.net/, not the usual url location. (and why
isn't the version @ http://poignantguide.net/ruby/ upto date or
redirecting? )

Becooz I don't like that blue yet. It buzzes people's eyelids too bad.

_why

Well, as long as it's a good reason...(and that's what style sheets are
for)

Ian