[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Can you make sense of this Array issue

seebs

5/26/2007 7:58:00 PM

In message <b034db400705261216x40b8da17xf2396c9013ac6dd2@mail.gmail.com>, "Roland Mai" writes:
>I want to have an array, say 5x5 with all nils unless set to something else.
>However, when I set m[0][0] to 0 .. all 0 indices are set to zero.. Hmm..
>never asked for that to be the case.
>There's some funky stuff going on here.

>irb(main):001:0> n = 5
>=> 5
>irb(main):002:0> m = [[nil]*n]*n

Nothing odd here, you're making an array containing the SAME n-ary array
n times.

-s

1 Answer

Paul Stickney

5/27/2007 12:24:00 AM

0

You might be interested in comparing:

Array.new(10, [])
and
Array.new(10) {[]}