# The scope of local variables -- where are they?

**URL:** <https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146>\
**Category:** Ruby\
**Created:** [September 8, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146 "2007-09-08T15:17:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 8, 2007, 3:17pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/1 "2007-09-08T15:17:04Z")

</div>

I’m wondering – where /is/ the scope of local variables? Is there any  
programmatic way to manipulate a local variable [by this, I rather  
awkwardly mean – for example, is there a hash on some object of local  
variables? Or something similar?] They seem special to me.

Cheers,  
Arlen

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 8, 2007, 3:34pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/2 "2007-09-08T15:34:05Z")

</div>

I wanna know how to do it so. Some method that receive a simple, just  
like  
const\_(get|set).

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 8, 2007, 3:58pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/3 "2007-09-08T15:58:07Z")

</div>

I know I can user defined? to test if they are defined, but is there any  
way  
to get them throught metaprogramming?

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 9, 2007, 4:44am UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/4 "2007-09-09T04:44:21Z")

</div>

On 9/8/07, Bernardo Monteiro R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I know I can user defined? to test if they are defined, but is there any way  
> to get them throught metaprogramming?

No (except for eval). What do you want to acheive? Whatever it is,  
there is probably another way, barring wanting to mess with local  
variables for the sake of messing with local variables.

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 8, 2007, 3:51pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/5 "2007-09-08T15:51:06Z")

</div>

On 08.09.2007 15:15, Arlen Christian Mart C. wrote:

> I’m wondering – where /is/ the scope of local variables? Is there any  
> programmatic way to manipulate a local variable [by this, I rather  
> awkwardly mean – for example, is there a hash on some object of local  
> variables? Or something similar?] They seem special to me.

There is a fairly easy way to manipulate them:

foo = 10

🙂

If your set of variables you need to manipulate is not fixed you better  
use a Hash directly. There are some ways to manipulate “unknown” local  
variables, but they typically involve using “eval”. You’ll find plenty  
of discussions of this topic in the archives.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 9, 2007, 5:52am UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/6 "2007-09-09T05:52:24Z")

</div>

On 09/09/2007, Logan C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> >

One use I could see for reflection on local variables is debugger  
support. You’ve run to a breakpoint; what is the full program state?

---

<div class="post-metadata">

**Author:** ![unnali](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/97f17d/32.png) [@unnali](https://www.ruby-forum.com/u/unnali)\
**Post date:** [September 9, 2007, 1:17pm UTC](https://www.ruby-forum.com/t/the-scope-of-local-variables-where-are-they/114146/7 "2007-09-09T13:17:54Z")

</div>

On 9/8/07, Michael R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> One use I could see for reflection on local variables is debugger  
> support. You’ve run to a breakpoint; what is the full program state?

That’s a pretty good example 🙂 (we do have local\_variables, but’s  
that is jsut an array of strings)
