# Why's (poignant) Guide to Ruby - code question

**URL:** <https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955>\
**Category:** Ruby\
**Created:** [June 7, 2011, 12:36am UTC](https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955 "2011-06-07T00:36:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [June 7, 2011, 12:36am UTC](https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955/1 "2011-06-07T00:36:00Z")

</div>

Hello,

I try to follow an example in why’s (poignant) Guide to Ruby. File  
‘wordlist.rb’ is created with the following content:

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

In another file this line:

require ‘wordlist’

and subsequent use of variable code\_words.

But code\_words gets flagged by the interpreter as an ‘undefined local  
variable’ and I cannot run the code.

How can I get variable code\_words in the desired scope?

---

<div class="post-metadata">

**Author:** ![datura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@datura](https://www.ruby-forum.com/u/datura)\
**Post date:** [June 7, 2011, 1:20am UTC](https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955/2 "2011-06-07T01:20:43Z")

</div>

Post an example program that is no longer than 5 lines that demonstrates  
the problem. Also state what what directory each file is in.

---

<div class="post-metadata">

**Author:** ![datura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@datura](https://www.ruby-forum.com/u/datura)\
**Post date:** [June 7, 2011, 1:31am UTC](https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955/3 "2011-06-07T01:31:24Z")

</div>

I found out that local variables in the required file are lost to the  
calling file. This fact is not very well described in The Ruby  
Programming Language (Flanagan, Matsumoto). Changing the first letter of  
code\_words to uppercase (ie Code\_words) makes it into a constant that  
CAN be accessed from the calling file

---

<div class="post-metadata">

**Author:** ![datura](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e274bd/32.png) [@datura](https://www.ruby-forum.com/u/datura)\
**Post date:** [June 7, 2011, 7:08pm UTC](https://www.ruby-forum.com/t/whys-poignant-guide-to-ruby-code-question/207955/4 "2011-06-07T19:08:38Z")

</div>

hey there Agent M. -

first, let me say how happy i am that you’re reading the poignant  
guide - brilliant freakin book. you’ll come away with a lot.

second, another option (and perhaps better than converting the hash  
into a constant,) is to put an “@” in front of code\_words, (@code\_words)

- this is an instance variable, and is also accessible from the calling  
file.
