public Method

Array.flatten

array.flatten  an_array

Returns a new array that is a one-dimensional flattening of this array (recursively). That is, for every element that is an array, extract its elements into the new array.

s = [ 1, 2, 3 ]           #=> [1, 2, 3]
t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10

Source Code

/*
*  call-seq:
*     array.flatten -> an_array
*  
*  Returns a new array that is a one-dimensional flattening of this
*  array (recursively). That is, for every element that is an array,
*  extract its elements into the new array.
*     
*     s = [ 1, 2, 3 ]           #=> [1, 2, 3]
*     t = [ 4, 5, 6, [7, 8] ]   #=> [4, 5, 6, [7, 8]]
*     a = [ s, t, 9, 10 ]       #=> [[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10]
*     a.flatten                 #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10
*/

static VALUE
rb_ary_flatten(ary)
   VALUE ary;
{
   ary = rb_ary_dup(ary);
   rb_ary_flatten_bang(ary);
   return ary;
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.