Proper use of 'require'

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::open( ‘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.

ZyLo wrote:

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

This is a bug in the tutorial. See
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152758

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.

ZyLo wrote:

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