# Real Beginner Question - (NameError)

**URL:** <https://www.ruby-forum.com/t/real-beginner-question-nameerror/89547>\
**Category:** Ruby\
**Created:** [March 21, 2007, 4:48am UTC](https://www.ruby-forum.com/t/real-beginner-question-nameerror/89547 "2007-03-21T04:48:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![CSZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@CSZ](https://www.ruby-forum.com/u/CSZ)\
**Post date:** [March 21, 2007, 4:48am UTC](https://www.ruby-forum.com/t/real-beginner-question-nameerror/89547/1 "2007-03-21T04:48:59Z")

</div>

Hi !  
I’m going my way through a Ruby online tutorial right now and i have  
following problem:

I have two files. File one:

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

makeSwamp.rb

#!/usr/bin/env ruby

require ‘wordlist’

# 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

When i execute the makeSwamp.rb file i can enter a “new idea” but than i  
get:

./makeSwamp.rb:8: undefined local variable or method `code\_words’ for  
main:Object (NameError)

(line 8 is: code\_words.each do |real, code| )

Why ??? I’ve run kdiff and its exactly as in the tutorial… Maybe its  
just to late for me…

---

<div class="post-metadata">

**Author:** ![CSZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@CSZ](https://www.ruby-forum.com/u/CSZ)\
**Post date:** [March 21, 2007, 5:02am UTC](https://www.ruby-forum.com/t/real-beginner-question-nameerror/89547/2 "2007-03-21T05:02:23Z")

</div>

On 3/21/07, CSZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi !  
> I’m going my way through a Ruby online tutorial right now and i have  
> following problem:

Hello. There is an unfortunate error in the design of that example.  
This exact question comes up from time to time on this list. (:

I searched the archives[1](http://groups.google.com/group/comp.lang.ruby/topics) for “Heat-Assisted Living” and came up with  
this thread:  
[http://rubyurl.com/FeV](http://rubyurl.com/FeV)

It looks to have some good answers in it.

Hope that helps,  
-Harold

---

<div class="post-metadata">

**Author:** ![CSZ](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/bcef8e/32.png) [@CSZ](https://www.ruby-forum.com/u/CSZ)\
**Post date:** [March 21, 2007, 5:05am UTC](https://www.ruby-forum.com/t/real-beginner-question-nameerror/89547/3 "2007-03-21T05:05:23Z")

</div>

On Mar 20, 11:49 pm, CSZ [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> When i execute the makeSwamp.rb file i can enter a “new idea” but than i  
> get:
> 
> /makeSwamp.rb:8: undefined local variable or method `code\_words’ for  
> main:Object (NameError)
> 
> (line 8 is: code\_words.each do |real, code| )
> 
> Why ??? I’ve run kdiff and its exactly as in the tutorial… Maybe its  
> just to late for me…

When you require (or load) a file, the local variables are not brought  
in. Try changing code\_words to a global variable by making it  
“$code\_words” (in both files). Or if you think it’s more appropriate,  
change it to a constant by making it “CodeWords” in both files.

Eric

Are you interested in on-site Ruby training that uses well-designed,  
real-world, hands-on exercises? [http://LearnRuby.com](http://LearnRuby.com)
