# Array

**URL:** <https://www.ruby-forum.com/t/array/137694>\
**Category:** IT\
**Created:** [May 15, 2008, 11:43am UTC](https://www.ruby-forum.com/t/array/137694 "2008-05-15T11:43:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 11:43am UTC](https://www.ruby-forum.com/t/array/137694/1 "2008-05-15T11:43:15Z")

</div>

Ciao a tutti, vi posto un problema sperando nel vostro gentile aiuto;

ho un array in cui devo aggiungere elementi dinamicamente in modo che  
raggiunga il formato:

rowtograph[0] = 1 , 5 , 8 , 0  
rowtograph[1] = 5 , 2 , 4 , 0

e cosÃ¬ via…

nessun problema definendolo cosÃ¬ come l’ho scritto sopra, il problema  
nasce volendolo costruire dinamicamente in questo modo:

rowtograph = []

for nuovoelemento in 0…5  
for elemento in 0…5  
rowtograph[elemento] \<\< aggiunta[nuovoelemento]  
end  
end

nelle mie intenzioni questo dovrebbe costruire l’array desiderato, in  
realtÃ produce il seguente errore:

NoMethodError (You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occurred while evaluating nil.\<\<)

grazie sin d’ora a chi vorrÃ aiutarmi

Stefano

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 11:52am UTC](https://www.ruby-forum.com/t/array/137694/2 "2008-05-15T11:52:47Z")

</div>

Forse intendi fare cosi?

rowtograph = []

for elemento in 0…5  
rowtograph \<\< aggiunta[elemento]  
end

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 11:53am UTC](https://www.ruby-forum.com/t/array/137694/3 "2008-05-15T11:53:21Z")

</div>

ma ci sono modi migliori in ruby per appendere un array ad un altro  
array

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 11:56am UTC](https://www.ruby-forum.com/t/array/137694/4 "2008-05-15T11:56:08Z")

</div>

On May 15, 2008, at 11:43 AM, Stefano B. wrote:

> nessun problema definendolo così come l’ho scritto sopra, il problema  
> nelle mie intenzioni questo dovrebbe costruire l’array desiderato, in  
> realtà produce il seguente errore:
> 
> NoMethodError (You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.\<\<)

Ti serve che rowtograph[elemento] abbia come valore iniziale un array  
vuoto, ma non  
lo inizializzi da nessuna parte. Puoi fare in 2 modi:

rowtograph = []  
for nuovoelemento in 0…5  
for elemento in 0…5  
(rowtograph[elemento] ||= []) \<\< aggiunta[nuovoelemento]  
end  
end

oppure, visto che la dimensione di rowtograph e’ nota:

rowtograph = Array.new(5) { Array.new }  
e il resto non cambia

S.

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 12:00pm UTC](https://www.ruby-forum.com/t/array/137694/5 "2008-05-15T12:00:03Z")

</div>

Claudio Petasecca D. wrote:

> Forse intendi fare cosi?
> 
> rowtograph = []
> 
> for elemento in 0…5  
> rowtograph \<\< aggiunta[elemento]  
> end

Ciao Claudio, grazie della tua risposta;

facendo come dici tu ogni elemento di rowtograph corrisponde ad un  
elemento di aggiunta, invece a me serve che ogni elemento di rowtograph  
contenga piÃ¹ elementi di aggiunta, ad es.

rowtograph[0] = 1 , 5 , 8 , 0  
rowtograph[1] = 5 , 2 , 4 , 0

cosa che mi riesce se definisco gli elementi staticamente (come  
nell’esempio) ma non mi riesce se cerco di farlo dinamicamente.

Che ne dici?

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 12:01pm UTC](https://www.ruby-forum.com/t/array/137694/6 "2008-05-15T12:01:56Z")

</div>

Stefano C. wrote:

> On May 15, 2008, at 11:43 AM, Stefano B. wrote:
> 
> > nessun problema definendolo cosï¿½ come l’ho scritto sopra, il problema  
> > nelle mie intenzioni questo dovrebbe costruire l’array desiderato, in  
> > realtï¿½ produce il seguente errore:
> > 
> > NoMethodError (You have a nil object when you didn’t expect it!  
> > You might have expected an instance of Array.  
> > The error occurred while evaluating nil.\<\<)
> 
> Ti serve che rowtograph[elemento] abbia come valore iniziale un array  
> vuoto, ma non  
> lo inizializzi da nessuna parte. Puoi fare in 2 modi:
> 
> rowtograph = []  
> for nuovoelemento in 0…5  
> for elemento in 0…5  
> (rowtograph[elemento] ||= []) \<\< aggiunta[nuovoelemento]  
> end  
> end
> 
> oppure, visto che la dimensione di rowtograph e’ nota:
> 
> rowtograph = Array.new(5) { Array.new }  
> e il resto non cambia
> 
> S.

Grazie mille Stefano!

provo subito il tuo suggerimento e ti faccio sapere.

Stefano

---

<div class="post-metadata">

**Author:** ![mex23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e47774/32.png) [@mex23](https://www.ruby-forum.com/u/mex23)\
**Post date:** [May 15, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/array/137694/7 "2008-05-15T12:29:32Z")

</div>

Stefano B. wrote:

> Stefano C. wrote:
> 
> > On May 15, 2008, at 11:43 AM, Stefano B. wrote:

> > Ti serve che rowtograph[elemento] abbia come valore iniziale un array  
> > vuoto, ma non  
> > lo inizializzi da nessuna parte. Puoi fare in 2 modi:
> > 
> > rowtograph = []  
> > for nuovoelemento in 0…5  
> > for elemento in 0…5  
> > (rowtograph[elemento] ||= []) \<\< aggiunta[nuovoelemento]  
> > end  
> > end
> > 
> > oppure, visto che la dimensione di rowtograph e’ nota:
> > 
> > rowtograph = Array.new(5) { Array.new }  
> > e il resto non cambia
> > 
> > S.

funziona perfettamente!

tutto risolto, grazie!
