Method: Array#pop

Defined in:
array.c

#popObject? #pop(n) ⇒ Object

Removes and returns trailing elements.

When no argument is given and self is not empty, removes and returns the last element:

a = [:foo, 'bar', 2]
a.pop # => 2
a # => [:foo, "bar"]

Returns nil if the array is empty.

When a non-negative Integer argument n is given and is in range, removes and returns the last n elements in a new Array:

a = [:foo, 'bar', 2]
a.pop(2) # => ["bar", 2]

If n is positive and out of range, removes and returns all elements:

a = [:foo, 'bar', 2]
a.pop(50) # => [:foo, "bar", 2]

Related: #push, #shift, #unshift.

Overloads:



1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
# File 'array.c', line 1396

static VALUE
rb_ary_pop_m(int argc, VALUE *argv, VALUE ary)
{
    VALUE result;

    if (argc == 0) {
	return rb_ary_pop(ary);
    }

    rb_ary_modify_check(ary);
    result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_LAST);
    ARY_INCREASE_LEN(ary, -RARRAY_LEN(result));
    ary_verify(ary);
    return result;
}