# Can't get 'require' to work

**URL:** <https://www.ruby-forum.com/t/cant-get-require-to-work/111605>\
**Category:** Ruby\
**Created:** [August 9, 2007, 1:57am UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605 "2007-08-09T01:57:30Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![drhowarddrfine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@drhowarddrfine](https://www.ruby-forum.com/u/drhowarddrfine)\
**Post date:** [August 9, 2007, 1:57am UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605/1 "2007-08-09T01:57:30Z")

</div>

I just started tinkering with Ruby. From “The Poignant Ruby P.mer”  
or whatever it’s called (you know what I mean) I tried running the small  
example but get an error.  
This is the first file called m2.rb:

require ‘/usr/home/me.rb’

# 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

And this is me.rb:

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’  
}

When I do: ruby me2.rb and enter any of the code\_words above, I get:

me2.rb:6: undefined local variable or method `code\_words’ for  
main:Object (NameError)

What am I missing?

---

<div class="post-metadata">

**Author:** ![drhowarddrfine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@drhowarddrfine](https://www.ruby-forum.com/u/drhowarddrfine)\
**Post date:** [August 9, 2007, 5:06am UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605/2 "2007-08-09T05:06:56Z")

</div>

On Aug 8, 2007, at 7:57 PM, Howard Fine wrote:

> print "Enter your new idea: "  
> end  
> }
> 
> When I do: ruby me2.rb and enter any of the code\_words above, I get:
> 
> me2.rb:6: undefined local variable or method `code\_words’ for  
> main:Object (NameError)

require is working fine. What you have is a scoping problem. The  
following shows a couple of ways to fix it.

\<code – me.rb\>  
$code\_words = {  
‘starmonkeys’ =\> ‘Phil and Pete, those prickly chancellors of the  
NewReich’,  
‘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’  
}

CODE\_WORDS = {  
‘starmonkeys’ =\> ‘Phil and Pete, those prickly chancellors of the  
NewReich’,  
‘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’  
}  
\</code?\>

\<code – me.rb\>  
require “/Users/mg/Desktop/me” # I put it in a different place, but  
makes no difference

# global variable

$code\_words # =\> {“Nigeria”=\>“Ny and Jerry’s Dry Cleaning (with  
Donuts)”, “firebomb”=\>“Heat-Assisted Living”, “catapult”=\>“chucky go-  
go”, “Put the kabosh on”=\>“Put the cable box on”,  
“starmonkeys”=\>“Phil and Pete, those prickly chancellors of the  
NewReich”}

# constant – note leading ::

::CODE\_WORDS # =\> {“Nigeria”=\>“Ny and Jerry’s Dry Cleaning (with  
Donuts)”, “firebomb”=\>“Heat-Assisted Living”, “catapult”=\>“chucky go-  
go”, “Put the kabosh on”=\>“Put the cable box on”,  
“starmonkeys”=\>“Phil and Pete, those prickly chancellors of the  
NewReich”}  
\</code?\>

Regards, Morton

---

<div class="post-metadata">

**Author:** ![drhowarddrfine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@drhowarddrfine](https://www.ruby-forum.com/u/drhowarddrfine)\
**Post date:** [August 9, 2007, 5:00pm UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605/3 "2007-08-09T17:00:23Z")

</div>

None of that worked.  
I think the Poignant Tutorial must have errors in it. I did change  
code\_words to $code\_words in the main file. That makes sense to me  
because it’s a global variable. But now I get the error " undefined  
method `each’ for nil:NilClass (NoMethodError)"

I haven’t read anything more than that tutorial so I need to get a  
better source. I do have one but liked this one for a quick and dirty  
overview.

---

<div class="post-metadata">

**Author:** ![drhowarddrfine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@drhowarddrfine](https://www.ruby-forum.com/u/drhowarddrfine)\
**Post date:** [August 9, 2007, 8:01pm UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605/4 "2007-08-09T20:01:50Z")

</div>

Howard Fine wrote:

> None of that worked.  
> I think the Poignant Tutorial must have errors in it. I did change  
> code\_words to $code\_words in the main file. That makes sense to me  
> because it’s a global variable. But now I get the error " undefined  
> method `each’ for nil:NilClass (NoMethodError)"
> 
> I haven’t read anything more than that tutorial so I need to get a  
> better source. I do have one but liked this one for a quick and dirty  
> overview.

make sure you change it to $code\_words in all instances 🙂

---

<div class="post-metadata">

**Author:** ![drhowarddrfine](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/59ef9b/32.png) [@drhowarddrfine](https://www.ruby-forum.com/u/drhowarddrfine)\
**Post date:** [August 9, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/cant-get-require-to-work/111605/5 "2007-08-09T20:30:11Z")

</div>

Roger P. wrote:

> Howard Fine wrote:
> 
> > None of that worked.  
> > I think the Poignant Tutorial must have errors in it. I did change  
> > code\_words to $code\_words in the main file. That makes sense to me  
> > because it’s a global variable. But now I get the error " undefined  
> > method `each’ for nil:NilClass (NoMethodError)"
> > 
> > I haven’t read anything more than that tutorial so I need to get a  
> > better source. I do have one but liked this one for a quick and dirty  
> > overview.
> 
> make sure you change it to $code\_words in all instances 🙂

Well, there ya’ go. Thanks!
