# Require

**URL:** <https://www.ruby-forum.com/t/require/98115>\
**Category:** Ruby\
**Created:** [July 12, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/require/98115 "2006-07-12T17:15:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2006, 5:15pm UTC](https://www.ruby-forum.com/t/require/98115/1 "2006-07-12T17:15:42Z")

</div>

I am using require in two different ways and they both don’t work.  
In case #1 I use it to read a file with a code snippet. The file is  
located in the same directory. The code snipped defines an array  
called `code_words'. When trying to print it it says" "undefined local variable or method`code\_words’ for main:Object  
(NameError)"

In the second case I have installed rubycoca and I’m trying to run the  
sample problem. I has a “require osx/cocoa” statement. The error  
message I get is “in `require’: no such file to load – osx/  
cocoa (LoadError)”

Searching the Internet I found that other people have the same second  
problem, but no answer was found on how to solve it.

Can anyone help?

Thanks ahead of time.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/require/98115/2 "2006-07-12T17:49:30Z")

</div>

Are you defining the code\_words variable as a local or instance  
variable. You should do:

bash\> cat code.rb  
@code\_words = [‘word1’, ‘word2’]

bash\> cat word.rb  
#!/usr/bin/env ruby

require ‘code’  
@code\_words += [‘word3’]  
puts @code\_words

bash\> ruby word.rb  
word1  
word2  
word3

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [July 12, 2006, 6:27pm UTC](https://www.ruby-forum.com/t/require/98115/3 "2006-07-12T18:27:03Z")

</div>

Piet Versteegen wrote:

> I am using require in two different ways and they both don’t work.  
> In case #1 I use it to read a file with a code snippet. The file is  
> located in the same directory. The code snipped defines an array  
> called `code_words'. When trying to print it it says" "undefined local variable or method`code\_words’ for main:Object  
> (NameError)"

Be easier to answer if you provided some code.

The issue is the local defined in the external file is not accessable  
in the scope where it is required. You can make the array global (call  
it ‘$code\_words’), or a constant (‘CODE\_WORDS’) or put it inside a  
module/class and access it appropriately.

> In the second case I have installed rubycoca and I’m trying to run the  
> sample problem. I has a “require osx/cocoa” statement. The error  
> message I get is “in `require’: no such file to load – osx/  
> cocoa (LoadError)”
> 
> Searching the Internet I found that other people have the same second  
> problem, but no answer was found on how to solve it.

That should just be a matter of ensuring the path to the osx dir is in  
the library path.  
You can speciy the path in the RUBYLIB environment variable, or on the  
ruby command line using -I or in your code by adding the path to $:

Cheers  
Chris
