instance method
Array#take
(::int n) -> ::Array[Elem]
take(count) → new_array
array.c
View on GitHub ↗
static VALUE
rb_ary_take(VALUE obj, VALUE n)
{
long len = NUM2LONG(n);
if (len < 0) {
rb_raise(rb_eArgError, "attempt to take negative size");
}
return rb_ary_subseq(obj, 0, len);
}
Returns a new array containing the first count element of self (as available); count must be a non-negative numeric; does not modify self:
Example
a = ['a', 'b', 'c', 'd'] a.take(2) # => ["a", "b"] a.take(2.1) # => ["a", "b"] a.take(50) # => ["a", "b", "c", "d"] a.take(0) # => []
Related: see Methods for Fetching.