# Referncing values of local variables

**URL:** <https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339>\
**Category:** Ruby\
**Created:** [November 17, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339 "2006-11-17T18:16:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 6:16pm UTC](https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339/1 "2006-11-17T18:16:57Z")

</div>

I apologize in advance, because I know this gets asked again and again  
(and again…) by newbies, but my searches don’t show a clear answer,  
and none of my attempts work.

This should be simple. I want to define a constant,

```
                         a = 3

```

then reference the value of a in arguments like,

```
               temp_array = Array.new(a)

```

such that the temp\_array is assigned a size of 3.

This always seems to result in complaints about undefined local variable  
or methods. Eval doesn’t seem to make it work.

What I am doing wrong??

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339/2 "2006-11-17T18:21:04Z")

</div>

> This should be simple. I want to define a constant,
> 
> ```
> a = 3
> 
> ```

In ruby, constant names begin with a capital letter (it is common  
practice to use fully uppercase names) :

MY\_CONSTANT=3

or

My\_constant=3

Variable names starting with a lowercase letter define local variables.

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 6:24pm UTC](https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339/3 "2006-11-17T18:24:48Z")

</div>

Hi –

On Sat, 18 Nov 2006, Thomas L. wrote:

> I apologize in advance, because I know this gets asked again and again  
> (and again…) by newbies, but my searches don’t show a clear answer,  
> and none of my attempts work.
> 
> This should be simple. I want to define a constant,
> 
> ```
> a = 3
> 
> ```

That’s not a constant; it’s a local variable. It sounds like you  
might be running into scoping issues.

> then reference the value of a in arguments like,
> 
> ```
> temp_array = Array.new(a)
> 
> ```
> 
> such that the temp\_array is assigned a size of 3.
> 
> This always seems to result in complaints about undefined local variable  
> or methods. Eval doesn’t seem to make it work.

I suspect you’re doing something like:

a = 3  
def my\_method  
temp\_array = Array.new(a)  
end

where a has gone out of scope by the time you use it.

You can use a constant:

A = 3

though that would normally be considered overkill, and bad design,  
unless it’s really a constant that needs to be defined outside of any  
method, rather than a local variable or method argument.

David

---

<div class="post-metadata">

**Author:** ![tlockney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5e9695/32.png) [@tlockney](https://www.ruby-forum.com/u/tlockney)\
**Post date:** [November 17, 2006, 6:25pm UTC](https://www.ruby-forum.com/t/referncing-values-of-local-variables/76339/4 "2006-11-17T18:25:01Z")

</div>

Stefano C. wrote:

> > This should be simple. I want to define a constant,
> > 
> > ```
> > a = 3
> > 
> > ```
> 
> In ruby, constant names begin with a capital letter (it is common  
> practice to use fully uppercase names) :
> 
> MY\_CONSTANT=3
> 
> or
> 
> My\_constant=3
> 
> Variable names starting with a lowercase letter define local variables.

Umm, uh (crawls under desk in embarassment…). Thanks. :}
