Method: Array#pop
- Defined in:
- array.c
#pop ⇒ Object? #pop(count) ⇒ Object
Removes and returns trailing elements of self
.
With no argument given, removes and returns the last element, if available; otherwise returns nil
:
a = [:foo, 'bar', 2]
a.pop # => 2
a # => [:foo, "bar"]
[].pop # => nil
With non-negative integer argument count
given, returns a new array containing the trailing count
elements of self
, as available:
a = [:foo, 'bar', 2]
a.pop(2) # => ["bar", 2]
a # => [:foo]
a = [:foo, 'bar', 2]
a.pop(50) # => [:foo, "bar", 2]
a # => []
Related: Array#push; see also Methods for Deleting.
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 |
# File 'array.c', line 1473
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;
}
|