# New to ruby, "requier" not working?

**URL:** <https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291>\
**Category:** Ruby\
**Created:** [December 29, 2005, 5:17am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291 "2005-12-29T05:17:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 29, 2005, 5:17am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/1 "2005-12-29T05:17:17Z")

</div>

Hello,

```
I'm trying to learn ruby from the Poignant guide to Ruby

```

([http://poignantguide.net/ruby/chapter-4.html](http://poignantguide.net/ruby/chapter-4.html)) and have run into a  
problem. In chapter 4 the simple word replacement example is the  
first use of the require statement. It does not work for me. I’m on  
OS 10.3, ruby 1.8.2 and my directory structure is :

~/projects/test.rb  
~/projects/wordlist.rb

test.rb starts with:  
require ‘wordlist’

and wordlist.rb just has:  
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’  
}

When i try and run test.rb it says that words is an undefined variable.  
If i put the definition of words directly into the test.rb file it  
works just fine.

I’m really frustrated with this so i could really use some advice.  
I’ve tried all the permutations of require “wordlist”, require  
“wordlist.rb” require ‘wordlist.rb’ require “./wordlist.rb” and  
nothing works. I can’t get much farther until i figure out how to get  
require working.

Thanks,  
Dave.

---

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 29, 2005, 6:21am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/2 "2005-12-29T06:21:31Z")

</div>

> ~/projects/test.rb  
> ~/projects/wordlist.rb
> 
> test.rb starts with:  
> require ‘wordlist’
> 
> and wordlist.rb just has:  
> 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’  
> }
> 
> When i try and run test.rb it says that words is an undefined variable.  
> If i put the definition of words directly into the test.rb file it  
> works just fine.

Dave, I am a newbie also. From the manual “require” and “load” are  
similar. Maybe try “load ‘wordlist.rb’”. Possibly your default  
directory is not ~/projects/ so the file cannot be found.

…Arch

---

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 29, 2005, 6:23am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/3 "2005-12-29T06:23:39Z")

</div>

Hi

Take a look at this thread:  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/a1324b2ea0f23839](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/a1324b2ea0f23839)

or via ruby-talk:  
[http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/100895?100721-101680](http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-talk/100895?100721-101680)

Clayton

---

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 31, 2005, 3:14am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/4 "2005-12-31T03:14:18Z")

</div>

See also:

[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/1adec0436f5d2b07](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/1adec0436f5d2b07)

---

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 31, 2005, 7:41am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/5 "2005-12-31T07:41:13Z")

</div>

On 12/29/05, Justin C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ~/projects/wordlist.rb  
> > ‘Nigeria’ =\> “Ny and Jerry’s Dry Cleaning (with Donuts)”,  
> > When you require a file, the local variables in that file stay in that  
> > scope, and are not imported into the current scope. To be able to access  
> > them, you must make them global.

actually, you could use an instance variable too @words = { … }  
works just fine. If you don’t intend to change it, use a constant.

Otherwise, you can use an instance variable which is generally better  
than a global.

sandal@karookachoo:~/Shared$ cat foo.rb  
@something = “hello”  
sandal@karookachoo:~/Shared$ ruby -e “require ‘foo’; puts @something”  
hello

Hope this helps  
-Greg

PS: Has anyone noticed that my code examples seem to come from a  
different shell EVERY time? I need to learn to stick to a machine for  
more than a week 🙂

---

<div class="post-metadata">

**Author:** ![David\_Just](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@David\_Just](https://www.ruby-forum.com/u/David_Just)\
**Post date:** [December 29, 2005, 6:25am UTC](https://www.ruby-forum.com/t/new-to-ruby-requier-not-working/51291/6 "2005-12-29T06:25:45Z")

</div>

David Just wrote:

> Hello,
> 
> I’m trying to learn ruby from the Poignant guide to Ruby  
> ([http://poignantguide.net/ruby/chapter-4.html](http://poignantguide.net/ruby/chapter-4.html)) and have run into a  
> problem. In chapter 4 the simple word replacement example is the  
> first use of the require statement. It does not work for me. I’m on  
> OS 10.3, ruby 1.8.2 and my directory structure is :
> 
> ~/projects/test.rb  
> ~/projects/wordlist.rb
> 
> test.rb starts with:  
> require ‘wordlist’
> 
> and wordlist.rb just has:  
> 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’  
> }

This has come up on the list before…  
‘words’ needs to be a global variable or constant. It’s basically a typo  
in the tutorial.  
Changing it to Words will get you a constant, changing it to $words will  
get you a global variable.

When you require a file, the local variables in that file stay in that  
scope, and are not imported into the current scope. To be able to access  
them, you must make them global.

-Justin
