Method: Enumerable#partition

Defined in:
enum.c

#partition {|obj| ... } ⇒ Array #partitionObject

Returns two arrays, the first containing the elements of enum for which the block evaluates to true, the second containing the rest.

If no block is given, an enumerator is returned instead.

(1..6).partition { |v| v.even? }  #=> [[2, 4, 6], [1, 3, 5]]

Overloads:

  • #partition {|obj| ... } ⇒ Array

    Yields:

    • (obj)

    Returns:



702
703
704
705
706
707
708
709
710
711
712
713
# File 'enum.c', line 702

static VALUE
enum_partition(VALUE obj)
{
    NODE *memo;

    RETURN_SIZED_ENUMERATOR(obj, 0, 0, enum_size);

    memo = NEW_MEMO(rb_ary_new(), rb_ary_new(), 0);
    rb_block_call(obj, id_each, 0, 0, partition_i, (VALUE)memo);

    return rb_assoc_new(memo->u1.value, memo->u2.value);
}