# Array range

**URL:** <https://www.ruby-forum.com/t/array-range/77334>\
**Category:** Ruby\
**Created:** [March 6, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/array-range/77334 "2006-03-06T17:11:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [March 6, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/array-range/77334/1 "2006-03-06T17:11:16Z")

</div>

Hi,

I have a question about assigning the contents of a list of variables  
with names like title1, title2, title3 up to title20 to an array.

Currently, this is the code that I use.

title1 = ruby  
title2 = rails  
title3 = typo  
.  
.  
.  
title20 = ajax

title\_array = [title1, title2, title3, title4, title5, title6, title7,  
title8, title9, title10, title11, title12, title13, title14, title15,  
title16, title17, title18, title19, title20]

title\_array.each do | i |  
do something…  
end

Is there a shorter way to do this? Instead of typing all the variable  
names, can I use a range to do this? Like title(1…20)?

Thanks for any help with this.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [March 6, 2006, 5:30pm UTC](https://www.ruby-forum.com/t/array-range/77334/2 "2006-03-06T17:30:09Z")

</div>

> Is there a shorter way to do this? Instead of typing all the variable  
> names, can I use a range to do this? Like title(1…20)?

You can use eval.

irb(main):106:0\> title1=“rails”  
=\> “rails”  
irb(main):107:0\> title2=“ruby”  
=\> “ruby”  
irb(main):108:0\> title3=“typo”  
=\> “typo”  
irb(main):117:0\> (1…3).each do |num|  
irb(main):118:1\* eval “puts title#{num}”  
irb(main):119:1\> end  
rails  
ruby  
typo

I’m not sure if there is a way to get a reference to an object if you  
know it’s symbol, but if there is that might be more elegant.

Farrel

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [March 6, 2006, 5:23pm UTC](https://www.ruby-forum.com/t/array-range/77334/3 "2006-03-06T17:23:58Z")

</div>

On Mar 6, 2006, at 11:14 AM, Paul T. wrote:

> .  
> end
> 
> Is there a shorter way to do this? Instead of typing all the variable  
> names, can I use a range to do this? Like title(1…20)?
> 
> Thanks for any help with this.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I am confused, why not title\_array = [ruby, rails, typo, …, ajax]

However if really want to do this, (I really think this needs a  
redesign though) you can do this:

title\_array = []

(1…20).each do |i|  
title\_array \<\< eval(“title#{i}”)  
end

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [March 6, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/array-range/77334/4 "2006-03-06T17:45:21Z")

</div>

Paul T. wrote:

> Hi,
> 
> I have a question about assigning the contents of a list of variables  
> with names like title1, title2, title3 up to title20 to an array.
> 
> Currently, this is the code that I use.
> 
> title1 = ruby  
> title2 = rails  
> title3 = typo

Where exactly is the array in your example? I’m a bit confused. Do you  
want this?

> > title = %w{ruby rails typo}  
> > =\> [“ruby”, “rails”, “typo”]

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [March 6, 2006, 5:59pm UTC](https://www.ruby-forum.com/t/array-range/77334/5 "2006-03-06T17:59:06Z")

</div>

Logan C. wrote:

> On Mar 6, 2006, at 11:14 AM, Paul T. wrote:
> 
> > .  
> > end
> > 
> > Is there a shorter way to do this? Instead of typing all the variable  
> > names, can I use a range to do this? Like title(1…20)?
> > 
> > Thanks for any help with this.
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> I am confused, why not title\_array = [ruby, rails, typo, …, ajax]
> 
> However if really want to do this, (I really think this needs a  
> redesign though) you can do this:
> 
> title\_array = []
> 
> (1…20).each do |i|  
> title\_array \<\< eval(“title#{i}”)  
> end

Thank you for everyone’s help with this. The eval method is what I was  
looking for. The contents for the variables are actually inside a  
database. I just use title1 = ruby as an example. Thanks again.
