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

I

Ian FalsePositives

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
 
D

David A. Black

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
 
D

Dominik Bathon

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
 
D

Dirk Meijer

------=_Part_1847_20090841.1126443651654
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

2005/9/11 said:
=20
On Sun, 11 Sep 2005 04:26:34 +0200, Ian FalsePositives

=20
This is an error in the guide, see
=20
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=20
exist.

------=_Part_1847_20090841.1126443651654--
 
I

Ian FalsePositives

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

Ian
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top