trouble with require

D

dave

i'm still a newbie doing every example in whys-poignant-guide-to-ruby,
i don't understand why code_words hash initialized in my wordlist.rb is
unavailable to my ruby program that requires it... the contains of
wordlist.rb : code_words={hash of code word}...and puts that prints it
out so i know that not only it is syntactly correct and initializes
code_words correct and it runs to completion when it 'requires' it
BUT... the next debug stmt (that i put in) of the ruby program doing
the requiring produces this error: codewordtranslator.rb:3: undefined
local variable or method `code_words' for main:Object (NameError)
Exit code: 1
and even in IRB same thing happens...why? .... what i'm doing wrong
here...trying to understand...tia dave
 
C

ChrisH

code_words={hash of code word}

The above defines a local variable. I don't know the details but it
looks like require sets the scope so that local vars are not
accessable. If you make your hash global:
$code_words={hash of code word}

than you can access it in an other file after require pulls it in.

Cheers
Chris
 
D

dave

i fixed the problem myself by rtfm a newer picax pdf book that i
bought....by redefining the contents of wordlist:
def kode_words
{hash here}
end
.....and in my calling rby program:
require 'wordlist'
code_words=kode_words() ....dave
 
T

Timothy Goddard

Generally only constants should be exposed after a require. Use a
capital letter at the start of the variable name and it should work.
Alternatively, have the required script store values in global
variables (beginning with a $ sign).
 

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

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top