Proper use of 'require'

Z

ZyLo

Hello. I have been trying to find this answer out for quite a while,
and I don't think I've found an exact answer to my problem.

I'm trying to use require to include a file that I wrote.
Specifically, I'm using Why's Poignant Guide to Ruby with Ruby 1.8.2 on
OS X 10.4.

http://www.poignantguide.net/ruby/chapter-4.html#section3


In his tutorial, he set up a file, wordlist.rb to contain a simple:

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 the file I'm calling this file, wordlist.rb is here:

require "wordlist"

print "Type and be diabolical: "
idea_backwards = gets.upcase.reverse

# Get evil idea and swap in code words
print "Enter your new idea: "
idea = gets
code_words.each do |real, code|
idea.gsub!( real, code )
end

# Save the jibberish to a new file
print "File encoded. Please enter a name for this idea: "
idea_name = gets.strip
File::eek:pen( 'idea-' + idea_name + '.txt', 'w' ) do |f|
f << idea
end


What happens when I run it, however, is that I get an error for a
NameError: undefined local variable or method `code_words' for
main:Object

I don't exactly know what I'm doing here, so I think I'm either missing
something, or the tutorial that is provided is outdated for the version
of Ruby i'm running it on.

The ONLY thing that is in the wordlist.rb file is that declaration for
code_words = { ... }. Am I missing something in here? Both files are
in the same relative directory, and I assumed that the file would be
loaded by first looking for it in the directory the script is run.

Any help would be appreciated. I really want to learn to use require
properly, as I don't want to write everything in the same file and I'd
like to split it up a little bit.

Thank You.
 
Z

ZyLo

Thank you very much. The updated version is excellent and had the exact
fix.

If anyone else is curious for what was wrong, you can't require a file
that has a variable in it, you can only import constants, so change
code_words to CODE_WORDS in both files and you're fine.
 
H

Hal Fulton

ZyLo said:
Thank you very much. The updated version is excellent and had the exact
fix.

If anyone else is curious for what was wrong, you can't require a file
that has a variable in it, you can only import constants, so change
code_words to CODE_WORDS in both files and you're fine.

Say it this way: If you require a file that has a local variable in it,
the local variable is not imported.


Hal
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top