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.