# Script local variables - the root of all mystery?

**URL:** <https://www.ruby-forum.com/t/script-local-variables-the-root-of-all-mystery/68738>\
**Category:** Ruby\
**Created:** [September 15, 2006, 12:40am UTC](https://www.ruby-forum.com/t/script-local-variables-the-root-of-all-mystery/68738 "2006-09-15T00:40:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jason\_Dusek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@Jason\_Dusek](https://www.ruby-forum.com/u/Jason_Dusek)\
**Post date:** [September 15, 2006, 12:40am UTC](https://www.ruby-forum.com/t/script-local-variables-the-root-of-all-mystery/68738/1 "2006-09-15T00:40:44Z")

</div>

I have a script with top level variables in it (i.e - variables that  
are not declared within a block, function definition or class  
definition). When I run this script through irb with load, I can  
access the variables just as though I had defined them in irb. Cool!

Then I added a true global variable - $opts - to my script. I run it  
through irb and, lo and behold, I can’t get at those top level  
variables anymore. What happenned? Did declaring the global variable  
somehow sink my script into its own context?

---

<div class="post-metadata">

**Author:** ![Jason\_Dusek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@Jason\_Dusek](https://www.ruby-forum.com/u/Jason_Dusek)\
**Post date:** [September 15, 2006, 12:42am UTC](https://www.ruby-forum.com/t/script-local-variables-the-root-of-all-mystery/68738/2 "2006-09-15T00:42:44Z")

</div>

## I don’t know why this gets sent twice. All my other proxied mail accounts behave fine.

\_jsn

---

<div class="post-metadata">

**Author:** ![Jason\_Dusek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/6f9a4e/32.png) [@Jason\_Dusek](https://www.ruby-forum.com/u/Jason_Dusek)\
**Post date:** [September 15, 2006, 5:37am UTC](https://www.ruby-forum.com/t/script-local-variables-the-root-of-all-mystery/68738/3 "2006-09-15T05:37:08Z")

</div>

Jason Dusek wrote:

> I have a script with top level variables in it (i.e - variables that  
> are not declared within a block, function definition or class  
> definition). When I run this script through irb with load, I can  
> access the variables just as though I had defined them in irb. Cool!
> 
> Then I added a true global variable - $opts - to my script. I run it  
> through irb and, lo and behold, I can’t get at those top level  
> variables anymore. What happenned? Did declaring the global variable  
> somehow sink my script into its own context?

I cannot exactly tell what went wrong in your case but I do know that  
IRB cannot be trusted once local variables are involved.

Maybe post the script so we get a better impression of what is going  
on?

Kind regards

robert
