# Metaprogramming - Array initialization

**URL:** <https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527>\
**Category:** Ruby\
**Created:** [April 24, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527 "2010-04-24T23:12:59Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 24, 2010, 11:12pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/1 "2010-04-24T23:12:59Z")

</div>

Hi,

In Ruby, we can declare array as explain in the doc:  
[http://ruby-doc.org/core/classes/Array.html#M002150](http://ruby-doc.org/core/classes/Array.html#M002150)

That makes possible many kind of use, such as:

> > Array.new(1, Array.new(3, Array.new(2, Array.new(1))))  
> > =\> [[[[nil], [nil]], [[nil], [nil]], [[nil], [nil]]]]

For my needs, I would like to use a code that declare an array of any  
arrays that we want. I don’t know if it is possible.

If the previous example can be generated using a function like:  
my\_array = make\_array(1, 3, 2, 1)

…this could be awesome!

Thank you for any comments and help.

Regards.

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 24, 2010, 11:48pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/2 "2010-04-24T23:48:36Z")

</div>

Paul A. wrote:

> arrays that we want. I don’t know if it is possible.  
> The above is probably not what you want. Look:

> > a=Array.new(3, Array.new(2))  
> > =\> [[nil, nil], [nil, nil], [nil, nil]]
> 
> > a[0][1] = 6  
> > =\> 6
> 
> > a  
> > =\> [[nil, 6], [nil, 6], [nil, 6]]

Try using the block form:

> > a=Array.new(3) {Array.new(2)}  
> > =\> [[nil, nil], [nil, nil], [nil, nil]]
> 
> > a[0][1] = 6  
> > =\> 6
> 
> > a  
> > =\> [[nil, 6], [nil, nil], [nil, nil]]

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 12:03am UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/3 "2010-04-25T00:03:50Z")

</div>

Agree with Joel. Use the block form of the new method, or you’ll get  
multiple references to the same object.  
This piece of code I believe does the trick. (No fault handling  
included,  
and probably not the most elegant, perhaps since I’m quite new to Ruby.)

def make\_array(\*a)  
a.flatten!  
if a.size \> 1  
Array.new(a.shift){make\_array(a)}  
elsif a.size == 1  
Array.new(a.shift)  
end  
end

irb(main):052:0\> make\_array(1, 3, 2, 1)  
=\> [[[[nil], [nil]], [[nil], [nil]], [[nil], [nil]]]]

Best regards,  
Rolf

On Sat, Apr 24, 2010 at 11:48 PM, Joel VanderWerf

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 12:19am UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/4 "2010-04-25T00:19:47Z")

</div>

Very instructives and helpful answers. Many thanks!

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 12:36am UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/5 "2010-04-25T00:36:02Z")

</div>

A subtle improvement to the function, as I failed to remember that I  
could  
expand an array to multiple arguments again when calling the function  
recursively:

def make\_array(\*a)  
if a.size \> 1  
Array.new(a.shift){make\_array(\*a)}  
elsif a.size == 1  
Array.new(a.shift)  
end  
end

-Rolf

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 12:40pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/6 "2010-04-25T12:40:34Z")

</div>

On 04/25/2010 12:35 AM, Rolf P. wrote:

> ```
> Array.new(a.shift)
> 
> ```
> 
> end  
> end

And what about the case of an empty argument list?

def make\_array(\*a)  
case a.size  
when 0  
raise ArgumentError, “Need at least one dimension”  
when 1  
Array.new(a.shift)  
else  
Array.new(a.shift){make\_array(\*a)}  
end  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 8:09pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/7 "2010-04-25T20:09:30Z")

</div>

Hi Joel,

You could construct the string that represents the command that would  
generate the multidimensional array creation and lauch it with  
instance\_eval.

def make\_array(input)

result = “”

for i in input  
result = result + “Array.new(#{i},”  
end

for i in input  
result = result + “)”  
end

instance\_eval(result)

end

The code is not very bright (and also a lot of checks on the input  
values  
should be made before executing instance eval) but I hope you get an  
idea.

Regards,  
Vicente

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [April 25, 2010, 10:48pm UTC](https://www.ruby-forum.com/t/metaprogramming-array-initialization/187527/8 "2010-04-25T22:48:05Z")

</div>

Had a small issue w/1.9.1.

Here is mine (inspired by Vicente’s code):

def multi\_array(\*input)  
instabce\_eval input.map {|i| “Array.new(#{i}”}.join(“,”) + ‘)’ \*  
input.length  
end

Definitely a cool way to make arrays.

Cheers,  
Ed

Ed Howland

> **[Green Programming - Blogging about Ruby, jQuery and Beyond!](https://greenprogrammer.wordpress.com/)**
>
> Just another WordPress.com weblog

[http://twitter.com/ed\_howland](http://twitter.com/ed_howland)
