# Creating Arrays Through a Loop

**URL:** <https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692>\
**Category:** Ruby\
**Created:** [February 1, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692 "2010-02-01T15:29:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 3:29pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/1 "2010-02-01T15:29:03Z")

</div>

I was wondering if it was possible to create a series of arrays through  
a loop. This doesn’t work (I think):

for num in 1…10  
array\_num = Array.new  
end

---

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 3:42pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/2 "2010-02-01T15:42:47Z")

</div>

Squawk B. wrote:

> I was wondering if it was possible to create a series of arrays through  
> a loop. This doesn’t work (I think):
> 
> for num in 1…10  
> array\_num = Array.new  
> end

Of course that doesn’t work. You’re setting the same variable  
(array\_num) each time. What you should do is use an array of arrays:

array\_of\_arrays = Array.new(10) {Array.new}

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 3:44pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/3 "2010-02-01T15:44:06Z")

</div>

Squawk B. wrote:

> I was wondering if it was possible to create a series of arrays through  
> a loop. This doesn’t work (I think):
> 
> for num in 1…10  
> array\_num = Array.new  
> end

It works fine, but it will always assign the new array to the same  
variable.

One idea is to create an array, and then add arrays as elements.

main\_array = Array.new  
(1…10).times { main\_array \<\< Array.new }

This may do what you want, but there may also be a more Ruby-ish way,  
depending on why you need those arrays.

---

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/4 "2010-02-01T17:41:38Z")

</div>

2010/2/1 Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> One idea is to create an array, and then add arrays as elements.
> 
> main\_array = Array.new  
> (1…10).times { main\_array \<\< Array.new }

Either use (1…10).each or use 10.times - but (1…10).times won’t work:

irb(main):002:0\> (1…10).times {|\*a| p a}  
NoMethodError: undefined method `times' for 1..10:Range from (irb):2 from /opt/bin/irb19:12:in `’

Cheers

robert

---

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 7:24pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/5 "2010-02-01T19:24:22Z")

</div>

Robert K. wrote:

> 2010/2/1 Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
> 
> > One idea is to create an array, and then add arrays as elements.
> > 
> > main\_array = Array.new  
> > (1…10).times { main\_array \<\< Array.new }
> 
> Either use (1…10).each or use 10.times - but (1…10).times won’t work:

Whoops! That’s what I get for crossing two thoughts.

---

<div class="post-metadata">

**Author:** ![squawkboxed](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/439d5e/32.png) [@squawkboxed](https://www.ruby-forum.com/u/squawkboxed)\
**Post date:** [February 1, 2010, 3:46pm UTC](https://www.ruby-forum.com/t/creating-arrays-through-a-loop/182692/6 "2010-02-01T15:46:51Z")

</div>

2010/2/1 Squawk B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I was wondering if it was possible to create a series of arrays through  
> a loop. This doesn’t work (I think):
> 
> for num in 1…10  
> array\_num = Array.new  
> end

Well, it _does_ work - only you loose references to a newly created  
Array immediately. You can try some of these variants:

arrs = []  
10.times { arrs \<\< Array.new }

arrs = Array.new(10) { Array.new }  
arrs = (1…10).map { Array.new }

Cheers

robert
