first(...)
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
/*
* call-seq:
* enum.first -> obj or nil
* enum.first(n) -> an_array
*
* Returns the first element, or the first +n+ elements, of the enumerable.
* If the enumerable is empty, the first form returns <code>nil</code>, and the
* second form returns an empty array.
*
*/
static VALUE
enum_first(argc, argv, obj)
int argc;
VALUE *argv;
VALUE obj;
{
VALUE n, ary[2];
if (argc == 0) {
ary[0] = ary[1] = Qnil;
}
else {
long len;
rb_scan_args(argc, argv, "01", &n);
len = NUM2LONG(n);
if (len == 0) return rb_ary_new2(0);
ary[0] = INT2NUM(len);
ary[1] = rb_ary_new2(len);
}
rb_block_call(obj, id_each, 0, 0, first_i, (VALUE)ary);
return ary[1];
}