From: Stefano Crocco Date: 2007-01-30T02:18:31+09:00 Subject: Re: classification Alle luned� 29 gennaio 2007, Josselin ha scritto: > I have to classify a bunch of products into 4 boxes, according to the > value of one of their attribute (product.code , an integer between 0 > and 4) > > I wrote that but it's seems wrong : > > box[0] = box[1] = box[2] = box[3] = [] > > for product in products > tag = Tag.new(product.title, product.price) > box[ product.code�] << tag > end > > then I'll reuse each box[] for further processing... > > what's wrong ? > > tfyl > > joss The problem is that writing something like a = b = c = [] will store the same array in all the variables. If you use the object_id methods on the four elements of box, you'll see that the returned value is the same. This means that modifiying, for instance box[0] will also modify box[1], box[2] and box[3], because they all contain the same object. You should replace the line box[0] = box[1] = box[2] = box[3] = [] with, for example, box=Array.new(4){[]} This will create a new Array with four elements, each of which is a different Array. Note that writing box=Array.new(4,[]) will produce the same (wrong) result as your code. I hope this helps Stefano