Method: Array#take

Defined in:
array.c

#take(count) ⇒ Object

Returns a new array containing the first count element of self (as available); count must be a non-negative numeric; does not modify self:

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.



7638
7639
7640
7641
7642
7643
7644
7645
7646
# File 'array.c', line 7638

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);
}