Se dive into the ruby source code, troveremo una funzione denominata chiamata quando array assignment happens con tre argomenti (ad esempio indice, la lunghezza e nuovo valore):
static VALUE
rb_ary_aset(int argc, VALUE *argv, VALUE ary)
{
long offset, beg, len;
if (argc == 3) {
rb_ary_modify_check(ary);
beg = NUM2LONG(argv[0]);
len = NUM2LONG(argv[1]);
rb_ary_splice(ary, beg, len, argv[2]);
return argv[2];
}
[...]
E se seguiamo avanti nel rb_ary_splice
faremo capita su dove avviene la magia:
static void
rb_ary_splice(VALUE ary, long beg, long len, VALUE rpl)
{
long rlen;
long olen;
if (len < 0) rb_raise(rb_eIndexError, "negative length (%ld)", len);
olen = RARRAY_LEN(ary);
[...]
if (len != rlen) {
RARRAY_PTR_USE(ary, ptr,
MEMMOVE(ptr + beg + rlen, ptr + beg + len,
VALUE, olen - (beg + len)));
ARY_SET_LEN(ary, alen);
}
if (rlen > 0) {
MEMMOVE(RARRAY_PTR(ary) + beg, RARRAY_CONST_PTR(rpl), VALUE, rlen);
}
}
RB_GC_GUARD(rpl);
}
in primo luogo si fa spazio nella matrice per i nuovi elementi e aggiorna la lunghezza:
01.235.
RARRAY_PTR_USE(ary, ptr,
MEMMOVE(ptr + beg + rlen, ptr + beg + len,
VALUE, olen - (beg + len)));
ARY_SET_LEN(ary, alen);
Poi attraverso la magia di C puntatori, si inserisce il nuovo elemento (s):
MEMMOVE(RARRAY_PTR(ary) + beg, RARRAY_CONST_PTR(rpl), VALUE, rlen);
Perché pensi che '[]' dovrebbe funzionare allo stesso modo come 'unshift'? Sono metodi diversi. – sawa
@sawa hai ragione, era solo una cosa, ma puoi spiegare [0,0] comportamento. È abbastanza diverso – ImranNaqvi
Credo che il documento per [Array # [\] =] (http://ruby-doc.org/core-2.2.0/Array.html#method-i-5B-5D-3D) sia piuttosto clear: "' ary [start, length] = obj' ... sostituisce un sottoarray [con gli elementi di 'obj'] dall'indice' start' per 'length' elements". Qui potresti trovare più chiaro scrivere 'arr.insert (0, arr2)'. (Se si desidera inserire "arr2" alla fine di "arr", è possibile scrivere "arr [arr.size, 0] = arr2' o" arr.insert (arr.size, * arr2) '). Notare il parallelo con [str \ [fixnum, fixnum \] = new_str] (http://ruby-doc.org/core-2.2.0/String.html#method-i-5B-5D-3D). –