flatten!(p1 = v1)
public
Flattens self in place.
Returns nil if no modifications were made (i.e., the array
contains no subarrays.)
The optional level argument determines the level of recursion to
flatten.
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten!
a.flatten!
a
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten!(1)
Show source
static VALUE
rb_ary_flatten_bang(int argc, VALUE *argv, VALUE ary)
{
int mod = 0, level = -1;
VALUE result, lv;
rb_scan_args(argc, argv, "01", &lv);
rb_ary_modify_check(ary);
if (!NIL_P(lv)) level = NUM2INT(lv);
if (level == 0) return Qnil;
result = flatten(ary, level, &mod);
if (mod == 0) {
ary_discard(result);
return Qnil;
}
if (!(mod = ARY_EMBED_P(result))) rb_obj_freeze(result);
rb_ary_replace(ary, result);
if (mod) ARY_SET_EMBED_LEN(result, 0);
return ary;
}