Forum: Ruby Proper use of 'require'

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
76c55292df5e9df9042eaf89ac35954b?d=identicon&s=25 ZyLo (Guest)
on 2005-12-31 12:45
(Received via mailing list)
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.
37a3c73ffbf864e4b28f7f2384ee12ce?d=identicon&s=25 Timothy Hunter (tim-hunter)
on 2005-12-31 12:51
(Received via mailing list)
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/...
76c55292df5e9df9042eaf89ac35954b?d=identicon&s=25 ZyLo (Guest)
on 2006-01-01 21:32
(Received via mailing list)
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.
C1bcb559f87f356698cfad9f6d630235?d=identicon&s=25 Hal Fulton (Guest)
on 2006-01-01 21:32
(Received via mailing list)
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
This topic is locked and can not be replied to.