# Proper use of 'require'

**URL:** <https://www.ruby-forum.com/t/proper-use-of-require/51448>\
**Category:** Ruby\
**Created:** [December 31, 2005, 12:45pm UTC](https://www.ruby-forum.com/t/proper-use-of-require/51448 "2005-12-31T12:45:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [December 31, 2005, 12:45pm UTC](https://www.ruby-forum.com/t/proper-use-of-require/51448/1 "2005-12-31T12:45:26Z")

</div>

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](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.

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [December 31, 2005, 12:51pm UTC](https://www.ruby-forum.com/t/proper-use-of-require/51448/2 "2005-12-31T12:51:32Z")

</div>

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](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](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/152758)

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [January 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/proper-use-of-require/51448/3 "2006-01-01T21:32:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ZyLo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/b3f665/32.png) [@ZyLo](https://www.ruby-forum.com/u/ZyLo)\
**Post date:** [January 1, 2006, 9:32pm UTC](https://www.ruby-forum.com/t/proper-use-of-require/51448/4 "2006-01-01T21:32:09Z")

</div>

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
