Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>limit is not an array method</li> <li>take requires an argument; it returns an empty array if the array is empty.</li> <li>first can be called without an argument; it returns nil if the array is empty and the argument is absent.</li> </ol> <p>Source for 2.0 take</p> <pre><code> static VALUE rb_ary_take(VALUE obj, VALUE n) { long len = NUM2LONG(n); if (len &lt; 0) { rb_raise(rb_eArgError, "attempt to take negative size"); } return rb_ary_subseq(obj, 0, len); } </code></pre> <p>Source for 2.0 first:</p> <pre><code> 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); } } </code></pre> <hr> <p>In terms of Rails:</p> <ol> <li><p><code>limit(5)</code> will add the scope of <code>limit(5)</code> to an <code>ActiveRecord::Relation</code>. <strong>It can not be called on an array</strong>, so <code>limit(5).limit(4)</code> will fail.</p></li> <li><p><code>first(5)</code> will add the scope of <code>limit(5)</code> to an <code>ActiveRecord::Relation</code>. <strong>It can also be called on an array</strong> so <code>.first(4).first(3)</code> will be the same as <code>.limit(4).first(3)</code>.</p></li> <li><p><code>take(5)</code> will run the query in the current scope, build all the objects and return the first 5. <strong>It only works on arrays</strong>, so <code>Model.take(5)</code> will not work, though the other two will work.</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload