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

22 views
Skip to first unread message

Ian FalsePositives

unread,
Sep 10, 2005, 10:23:27 PM9/10/05
to
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
www.FalsePositives.com

David A. Black

unread,
Sep 10, 2005, 10:37:56 PM9/10/05
to
Hi --

On Sun, 11 Sep 2005, Ian FalsePositives 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?!!

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

--
David A. Black
dbl...@wobblini.net


Dominik Bathon

unread,
Sep 11, 2005, 8:44:07 AM9/11/05
to
On Sun, 11 Sep 2005 04:26:34 +0200, Ian FalsePositives
<Ian.I...@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?!!

This is an error in the guide, see

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


Dirk Meijer

unread,
Sep 11, 2005, 9:00:54 AM9/11/05
to
2005/9/11, Dominik Bathon <dba...@gmx.de>:

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

Ian FalsePositives

unread,
Sep 12, 2005, 7:17:06 PM9/12/05
to
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

why the lucky stiff

unread,
Sep 12, 2005, 7:32:03 PM9/12/05
to
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


Ian FalsePositives

unread,
Sep 12, 2005, 8:30:36 PM9/12/05
to
Well, as long as it's a good reason...(and that's what style sheets are
for)

Ian

Reply all
Reply to author
Forward
0 new messages