From: Vicente Bosch Date: 2010-04-26T03:08:44+09:00 Subject: Re: Metaprogramming - Array initialization --001517477fb0d8c02c0485138c78 Content-Type: text/plain; charset=UTF-8 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/. > > --001517477fb0d8c02c0485138c78--