first(p1 = v1)
public
Returns the first element, or the first n elements, of the
enumerable. If the enumerable is empty, the first form returns nil, and the second
form returns an empty array.
Show source
static VALUE
enum_first(int argc, VALUE *argv, VALUE obj)
{
VALUE n, params[2];
if (argc == 0) {
params[0] = params[1] = Qnil;
}
else {
long len;
rb_scan_args(argc, argv, "01", &n);
len = NUM2LONG(n);
if (len == 0) return rb_ary_new2(0);
if (len < 0) {
rb_raise(rb_eArgError, "negative length");
}
params[0] = len;
params[1] = rb_ary_new2(len);
}
rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)params);
return params[1];
}