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