# Simple scope question

**URL:** <https://www.ruby-forum.com/t/simple-scope-question/49593>\
**Category:** Ruby\
**Created:** [December 6, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/simple-scope-question/49593 "2005-12-06T19:07:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nospam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c0e974/32.png) [@nospam](https://www.ruby-forum.com/u/nospam)\
**Post date:** [December 6, 2005, 7:07pm UTC](https://www.ruby-forum.com/t/simple-scope-question/49593/1 "2005-12-06T19:07:07Z")

</div>

## Is it possible to place another file within the same scope as another file that loads it? For example, I can’t get this to work:

#!/usr/bin/env ruby

# file1.rb

## file1 = “something”

#!/usr/bin/env ruby

# file2.rb

## load ‘file1.rb’ puts file1

C:/\>file2.rb  
C:/file2.rb:4: undefined local variable or method `file1’ for  
main:Object (NameError)

Of course, I could make them global variables, but it would be nice if I  
could load it  
in with the same scope.

Any ideas?  
Thanks!  
– Glenn Lewis

---

<div class="post-metadata">

**Author:** ![nospam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c0e974/32.png) [@nospam](https://www.ruby-forum.com/u/nospam)\
**Post date:** [December 6, 2005, 7:35pm UTC](https://www.ruby-forum.com/t/simple-scope-question/49593/2 "2005-12-06T19:35:38Z")

</div>

On Wed, 7 Dec 2005, Glenn M. Lewis wrote:

> load ‘file1.rb’  
> Any ideas?  
> i assume you are making a config file? why not use yaml?

-a

---

<div class="post-metadata">

**Author:** ![nospam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c0e974/32.png) [@nospam](https://www.ruby-forum.com/u/nospam)\
**Post date:** [December 6, 2005, 8:00pm UTC](https://www.ruby-forum.com/t/simple-scope-question/49593/3 "2005-12-06T20:00:12Z")

</div>

Glenn M. Lewis wrote:

> load ‘file1.rb’
> 
> Any ideas?  
> Thanks!  
> – Glenn Lewis

The best solution I’ve found is to read file1.rb and module\_eval it’s  
contents in the scope of a new module. Then, if you’ve defined constants  
and methods in file1.rb, you can access them through the module. You  
can’t get the local vars this way (which may be a good thing). (See the  
script project on RAA for an elaboration of this approach.)

It’s also possible to assign to the local vars before loading file1.rb,  
read file1.rb, eval it, and then the local variables will be updated.  
But you have to know in advance what local variables file1.rb has:

ruby -e “x=1; eval ‘x=2;y=3’; p x, y”  
-e:1: undefined local variable or method `y’ for main:Object (NameError)

ruby -e “x=1; eval ‘x=2;y=3’; p x”  
2

---

<div class="post-metadata">

**Author:** ![nospam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c0e974/32.png) [@nospam](https://www.ruby-forum.com/u/nospam)\
**Post date:** [December 7, 2005, 12:01am UTC](https://www.ruby-forum.com/t/simple-scope-question/49593/4 "2005-12-07T00:01:05Z")

</div>

## You can’t get quite the same scope. Check out Joel’s “Script” library [http://raa.ruby-lang.org/project/script/0.2](http://raa.ruby-lang.org/project/script/0.2) or use YAML or maybe even something simple like this:

#!/usr/bin/env ruby

# file1.rb

## def file1() “\nCheers,\nDave” end

#!/usr/bin/env ruby

# file2.rb

## eval File.read(‘file1.rb’) puts file1

C:/\>file2.rb

Cheers,  
Dave
