/*
 *  call-seq:
 *     array.first   ->   obj or nil
 *     array.first(n) -> an_array
 *
 *  Returns the first element, or the first +n+ elements, of the array.
 *  If the array is empty, the first form returns <code>nil</code>, and the
 *  second form returns an empty array.
 *
 *     a = [ "q", "r", "s", "t" ]
 *     a.first    #=> "q"
 *     a.first(1) #=> ["q"]
 *     a.first(3) #=> ["q", "r", "s"]
 */

static VALUE
rb_ary_first(argc, argv, ary)
    int argc;
    VALUE *argv;
    VALUE ary;
{
    if (argc == 0) {
        if (RARRAY(ary)->len == 0) return Qnil;
        return RARRAY(ary)->ptr[0];
    }
    else {
        VALUE nv, result;
        long n, i;

        rb_scan_args(argc, argv, "01", &nv);
        n = NUM2LONG(nv);
        if (n > RARRAY(ary)->len) n = RARRAY(ary)->len;
        result = rb_ary_new2(n);
        for (i=0; i<n; i++) {
            rb_ary_push(result, RARRAY(ary)->ptr[i]);
        }
        return result;
    }
}