javascript - What is an "invalid slice size" error in rails? - @categories.each_slice(column_length) do |column| Happens w/ this code. -
Getting "illegal slice size"
New brand for rail playing around the front end
- column_length = @ categories.length.fdiv (3) .ceil - @ categories.each_slice (column_length) do. Column |
This code generates an error It seems that working fine on the ongoing site only I have thought that the Navy and Bootstrap layout have been edited so far ..
What could be the cause of this error, which is keeping page form rendering?
OK, I have reproduced this error again:
< Code> [1,2, 3] .each_slice (0.22222222) .to_a # argumentError: invalid piece size [1,2,3] .each_slice (0) .to_a # logic error: invalid slice size [1,2,3] .each_slice (-1) To_a # ArgumentError: Invalid slice size
#Is_Slice
as its code, except for Fixnum
examples, 0 negative number
, then it will give you the following type: #is_slus
always tries to convert its logic to Fixnum
by using the method, And after doing this, if you get it 0
or any negative number , you get the error as you got it. And if the argument can not be converted to integration
[1,2,3] .each_slice ([1]). To_a # TypeError: There is no built-in conversion of an array in [1,2,3] .each_slice (Object.new) .to_a # TypeError: no underlying conversion of objects in the object
No But you have given it the float
dot number below like typing after a Fixnum
example, but not 0
, Will work fine:
[1,2,3] .each_slice (2.22222222) .to_a # = & gt; [[1, 2], [3]]
Now, in your case, @ categories.length.fdiv (3) .ceil
gives you something < Code> 0 or negative number , and the reasoning value is due to # east_slice
error
OK, let me see the C Code for
from
Fixed VALUE enum_each_slice (VALUE obj, VALUE n) {long size = NUM2LONG (n); VALUE Arya; NODE * memo; Int arity; If (size & lt; = 0) rb_raise (rb_eArgError, "illegal piece size"); RETURN_SIZED_ENUMERATOR (OBG, 1, & N, ENUM_H_Sallis_size); Array = RB_ERI_NU2 (size); Arity = rb_block_arity (); Memo = NEW_MEMO (Arya, dont_recycle_block_arg (eraity), size); Rb_block_call (obj, id_each, 0, 0, each_slici_i, (VALUE) memo); Array = memo-> U1.value; If (RARRAY_LEN (Arya)> 0) Rb_Ild (Arya); Return Qnil; }
Then, why [1,2,3] .each_slice (0.22222222) .to_a
caused by error - argument error: invalid slice size < / Code>:
OK, you can see from MRI source code that n
(this means 0.22222222
) function NUM2LONG ( )
. Now, if you look at the function, you will get
def self.num2long (obj) if obj == zero type type error, "no absolute This code is very clearly saying, what's happening inside the NUM2LONG ()
function is IRB: the conversion is not "and the integer (Obj) end and
integer (0.22222) # = & gt; In, to reproduce the error, as I used in my top 3 examples, now 3 examples can try the n
value. ; 0 integer (-1) # = & gt; -1 integer (0) # = & gt; 0
Assigning 0
to n = 0.22222
in MRI size
function < Code> NUM2LONG (n) . It turns on the zero value error.
Comments
Post a Comment