# Calling Variable in the "fly" \*inside loop

**URL:** <https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272>\
**Category:** Ruby\
**Created:** [June 7, 2014, 11:40pm UTC](https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272 "2014-06-07T23:40:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jason\_sam](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/7993a0/32.png) [@jason\_sam](https://www.ruby-forum.com/u/jason_sam)\
**Post date:** [June 7, 2014, 11:40pm UTC](https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272/1 "2014-06-07T23:40:36Z")

</div>

Hello there, I’m self though beginner programmer in ruby, and sometimes  
I came up with strange ideas that sometimes work and sometimes not.

Today I have something I call “constructing variable in fly”, while  
Looping i use this number as construct the name of variable.

say I have var1, var2, var3 = “mouse”,“cat”,“dog”

for i in 1…3  
puts var + i.to\_s  
end

This doesn’t work, I know it doesn’t work but I was wondering why?

Thanks for your time reading this.

---

<div class="post-metadata">

**Author:** ![zetticci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/85f322/32.png) [@zetticci](https://www.ruby-forum.com/u/zetticci)\
**Post date:** [June 8, 2014, 12:58am UTC](https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272/2 "2014-06-08T00:58:26Z")

</div>

The error is:

```
 undefined local variable or method `var'

```

When ruby sees this code:

```
puts var + i.to_s

```

It first has to execute the part:

```
var + i.to_s

```

And to execute that part, ruby fhas to substitute the values for var and  
i.to\_s into that line of code. Ruby first tries to figure out what the  
value of var is. var can either be the name of a variable or it can be a  
method call,  
e.g.:

def var  
puts ‘hi’  
end

var

–output:–  
hi

Because ruby cannot find a variable named var, nor a def named var, ruby  
has no idea what you mean by var, hence the error message.

Your attempt is a common beginner attempt. When you write a program,  
you will NEVER variable names that differ only by a  
number–that is what arrays are for. The names of the variables are  
var[0], var[1], var[2], etc. instead of var1, var2, var3.

You could use the eval() method to do what you want. eval() takes a  
string that looks like code as an argument and then executes the  
string/code:

var1, var2, var3 = “mouse”,“cat”,“dog”

for i in 1…3  
eval “puts var#{i}”  
end

–output:–  
mouse  
cat  
dog

However, you should start your studies of computer programming with the  
following dictate: NEVER USE EVAL.

---

<div class="post-metadata">

**Author:** ![zetticci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/85f322/32.png) [@zetticci](https://www.ruby-forum.com/u/zetticci)\
**Post date:** [June 8, 2014, 3:51am UTC](https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272/3 "2014-06-08T03:51:01Z")

</div>

7stud, you are one awsome dude, thanks man I learn a lot. I’ll remember  
to not to use eval lol.

---

<div class="post-metadata">

**Author:** ![zetticci](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/85f322/32.png) [@zetticci](https://www.ruby-forum.com/u/zetticci)\
**Post date:** [June 8, 2014, 1:24pm UTC](https://www.ruby-forum.com/t/calling-variable-in-the-fly-inside-loop/230272/4 "2014-06-08T13:24:53Z")

</div>

Ivana Zetticci wrote in post #1149167:

> 7stud, you are one awsome dude, thanks man I learn a lot. I’ll remember  
> to not to use eval lol.

I don’t get it: why is that funny?

Cheers

robert
