Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Real Beginner Question - (NameError)

Reply
Thread Tools

Real Beginner Question - (NameError)

 
 
CSZ
Guest
Posts: n/a
 
      03-21-2007
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:pen( "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..

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
Harold Hausman
Guest
Posts: n/a
 
      03-21-2007
On 3/21/07, CSZ <> 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] for "Heat-Assisted Living" and came up with
this thread:
http://rubyurl.com/FeV

It looks to have some good answers in it.

Hope that helps,
-Harold

[1]: http://groups.google.com/group/comp.lang.ruby/topics

 
Reply With Quote
 
 
 
 
Eric I.
Guest
Posts: n/a
 
      03-21-2007
On Mar 20, 11:49 pm, CSZ <chsch...@uos.de> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Beginner: Trying to get REAL NUMBERS from %d command Alvaro Lacerda Python 6 12-30-2012 11:23 PM
Beginner's Beginner william nelson Ruby 7 04-11-2011 11:23 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM
adaware-real beginner question brn2rnjk1@aol.com Computer Support 24 03-06-2004 05:24 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57