J
Johan Holmberg
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}
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.
Have I missed some other method that does this in a readable way ?
/Johan Holmberg
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}
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.
Have I missed some other method that does this in a readable way ?
/Johan Holmberg