Arrays in Ruby: Take vs Limit vs First
- limit is not an array method
- take requires an argument; it returns an empty array if the array is empty.
- first can be called without an argument; it returns nil if the array is empty and the argument is absent.
Source for 2.0 take
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);
}
Source for 2.0 first:
static VALUE
rb_ary_first(int argc, VALUE *argv, VALUE ary)
{
if (argc == 0) {
if (RARRAY_LEN(ary) == 0) return Qnil;
return RARRAY_PTR(ary)[0];
}
else {
return ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
}
}
In terms of Rails:
limit(5)
will add the scope oflimit(5)
to anActiveRecord::Relation
. It can not be called on an array, solimit(5).limit(4)
will fail.first(5)
will add the scope oflimit(5)
to anActiveRecord::Relation
. It can also be called on an array so.first(4).first(3)
will be the same as.limit(4).first(3)
.take(5)
will run the query in the current scope, build all the objects and return the first 5. It only works on arrays, soModel.take(5)
will not work, though the other two will work.
The answer chosen answer seems to be outdated (in terms of Rails) so I would like to update some information.
limit
on anActiveRecord::Relation
will still be aRelation
. So if you call:Model.limit(5).limit(4)
will be same as:
Model.limit(4)
first
on anActiveRecord::Relation
will make the result anArray
. So you cannot call any scope afterfirst
like:Model.first(5).where(id: 1)
But you can do this:
Model.limit(5).where(id: 1)
take
Model.take(5)
works now. It will return an array, so you cannot call any
scope
either.
On an ActiveRecord::Relation
object, if you call first
it will include ORDER BY 'tables'.id
. Whereas with limit
and take
there is no ORDER BY
included but the sorting is depended by the database sorting implementation.