flatten(...)
public
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. If the optional level
argument determines the level of recursion to flatten.
s = [ 1, 2, 3 ]
t = [ 4, 5, 6, [7, 8] ]
a = [ s, t, 9, 10 ]
a.flatten
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten(1)
Show source
/*
* call-seq:
* array.flatten -> an_array
* array.flatten(level) -> 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. If the optional
* <i>level</i> argument determines the level of recursion to flatten.
*
* s = [ 1, 2, 3 ]
* t = [ 4, 5, 6, [7, 8] ]
* a = [ s, t, 9, 10 ]
* a.flatten
* a = [ 1, 2, [3, [4, 5] ] ]
* a.flatten(1)
*/
static VALUE
rb_ary_flatten(argc, argv, ary)
int argc;
VALUE *argv;
VALUE ary;
{
int mod = 0, level = -1;
VALUE result, lv;
rb_scan_args(argc, argv, "01", &lv);
if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return ary;
result = flatten(ary, level, &mod);
if (OBJ_TAINTED(ary)) OBJ_TAINT(result);
return result;
}