From: Ed Howland Date: 2010-04-26T05:47:17+09:00 Subject: Re: Metaprogramming - Array initialization 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 http://greenprogrammer.wordpress.com http://twitter.com/ed_howland On Sun, Apr 25, 2010 at 2:08 PM, Vicente Bosch wrote: > 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 > > On 24 April 2010 23:13, Paul A. wrote: > >> Hi, >> >> In Ruby, we can declare array as explain in the doc: >> 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. >>  -- >> Posted via http://www.ruby-forum.com/. >> >> >