[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

trouble with require

dave

6/21/2006 2:36:00 PM

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

3 Answers

Chris Hulan

6/21/2006 3:03:00 PM

0

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

dave

6/21/2006 6:24:00 PM

0

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
ChrisH wrote:
> 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

Timothy Goddard

6/25/2006 6:06:00 AM

0

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

dave wrote:
> 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