# Require not working correctly?

**URL:** <https://www.ruby-forum.com/t/require-not-working-correctly/168448>\
**Category:** Ruby\
**Created:** [May 20, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448 "2009-05-20T22:15:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 20, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/1 "2009-05-20T22:15:54Z")

</div>

I’m just starting out in Ruby, and I used the one-click installer. The  
version on my computer seems to be Ruby 1.8.6-27.

I’m working through some of the examples in Why’s Poignant Guide, and  
things seem to have broken down here:

File wordlist.rb contains:  
puts ‘Wordlist got required’  
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’  
}

File script.rb contains:  
require ‘wordlist’

# Get evil idea and swap in code words

puts "Enter your new idea: "  
idea = gets  
code\_words.each do |real, code|  
idea.gsub!(real, code)  
end

# Save the jibberish to a new file

puts "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

This is basically copied out of the Poignant Guide, with the addition  
of the puts method at the start of wordlist.rb.

Both are in the same directory. I navigate to that directory in cmd,  
and then type ruby script.rb

It prints Wordlist got required, then prompts for an evil idea, and  
after I hit enter, gives the following error:  
script.rb:5: undefined local variable or method `code\_words’ for  
main:Object (NameError)

What am I doing wrong?

Thanks,

- Adlai

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 20, 2009, 10:44pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/2 "2009-05-20T22:44:36Z")

</div>

> File script.rb contains:  
> require ‘wordlist’

> It prints Wordlist got required, then prompts for an evil idea, and  
> after I hit enter, gives the following error:  
> script.rb:5: undefined local variable or method `code\_words’ for  
> main:Object (NameError)
> 
> What am I doing wrong?

The scripts operate each in its own “scope” so setting a variable in one  
doesn’t set it in the other. Kind of confusing at first, but at least  
you don’t have to worry about leaked variables from one script to the  
next.  
To avoid it save it to a global like  
$code\_words  
or put it all in one .rb file.  
GL!  
-=r

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 20, 2009, 11:26pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/3 "2009-05-20T23:26:01Z")

</div>

On May 20, 11:44 pm, Roger P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> doesn’t set it in the other. Kind of confusing at first, but at least  
> you don’t have to worry about leaked variables from one script to the  
> next.  
> To avoid it save it to a global like  
> $code\_words

I figured out this trick myself, but globals could cause problems in  
larger projects with name collisions…

> ## or put it all in one .rb file. GL! -=r
> 
> Posted viahttp://www.ruby-forum.com/.

Thanks for your help. Just wanted to make sure that I wasn’t making  
some newbie mistake.

- Adlai

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 20, 2009, 11:40pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/4 "2009-05-20T23:40:39Z")

</div>

Roger P. wrote:

> > I figured out this trick myself, but globals could cause problems in  
> > larger projects with name collisions…
> 
> The only way to share things across scopes is constants or globals–that  
> I know of 🙂

You can also write your own #load method, and use the return value to  
access things defined in the loaded file. It’s actually quite easy to do  
this. Here’s the approach I use:

[http://redshift.sourceforge.net/script/](http://redshift.sourceforge.net/script/)

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 22, 2009, 6:31am UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/5 "2009-05-22T06:31:42Z")

</div>

On May 21, 12:39 am, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [Script](http://redshift.sourceforge.net/script/)
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thank you Joel. I’ll let you know how I like your Script thingy after  
I play around with it later today.

- Adlai

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 23, 2009, 3:00am UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/6 "2009-05-23T03:00:03Z")

</div>

Does this work with local variables too?

For example, lets say I have a file called:

foobar.rb

Inside it we could have a class

class Foo  
def test  
puts ‘hi from class Foo’  
end  
end

foo\_object = Foo.new

Now, I would like to use this variable  
foo\_object  
in another ruby file, or context.

But as far as I know without eval this was not possible.

Is this somehow possible with ‘script’? (My brain is a bit confused  
right now.)

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 20, 2009, 11:29pm UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/7 "2009-05-20T23:29:47Z")

</div>

> I figured out this trick myself, but globals could cause problems in  
> larger projects with name collisions…

The only way to share things across scopes is constants or globals–that  
I know of 🙂

So you could rename it CODE\_WORDS and that would work.  
-=r

---

<div class="post-metadata">

**Author:** ![Adlai](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d78d45/32.png) [@Adlai](https://www.ruby-forum.com/u/Adlai)\
**Post date:** [May 23, 2009, 4:48am UTC](https://www.ruby-forum.com/t/require-not-working-correctly/168448/8 "2009-05-23T04:48:01Z")

</div>

Marc H. wrote:

> ```
> puts 'hi from class Foo'
> 
> ```
> 
> But as far as I know without eval this was not possible.
> 
> Is this somehow possible with ‘script’? (My brain is a bit confused  
> right now.)

With ‘script’, you can only export constants and methods. So you could  
do this:

FOO\_OBJECT = Foo.new

and then your main file can do this:

my\_script = Script.load(“foobar.rb”)  
p my\_script::FOO\_OBJECT

Even though you are defining a constant, it is accessible only via the  
object assigned to my\_script (the object is in fact a module), so you  
don’t have to worry about namespace pollution.

HTH…
