From: Robert Klemme Date: 2004-01-15T00:51:44+09:00 Subject: Re: Concatenating variable number of Arrays ? "Johan Holmberg" schrieb im Newsbeitrag news:Pine.GSO.4.50.0401141612480.29527-100000@pjakkur.iar.se... > > Hi! > > I have been searching for a method to concatenate several arrays. > A had an Array like this > > arr1 = [ [1,2,3], [4,5,6], [7,[8,9]] ] > > and wanted to produce > > arr2 = [1, 2, 3, 4, 5, 6, 7, [8, 9]] > > Looking at the methods of the Array class I found some methods that > do similar things, but none that fits exactly: > > + concatenate TWO Arrays > > concat concatenate TWO arrays destructively modifying > the first > > flatten RECURSIVELY flattens elements into a new Array > > > The best way I have found until now, of doing what I want is > > arr2 = arr1.inject([]) {|acc,x| acc.concat x} That's exactly what I'd do. > But I feel that the thing I want to do (concatenating a variable > number arrays) is such a natural thing, that it deserves a cleaner > way of being expressed. All other ways that I can think of are more verbose and not necessarily cleander - let alone faster. The version using each would look like arr2=[] arr1.each {|x| arr2.concat x} Not really an improvement IMHO. > Have I missed some other method that does this in a readable way ? I don't think so. robert