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
dblack / wobblini.net