# Classification

**URL:** <https://www.ruby-forum.com/t/classification/83778>\
**Category:** Ruby\
**Created:** [January 29, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/classification/83778 "2007-01-29T18:08:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 29, 2007, 6:08pm UTC](https://www.ruby-forum.com/t/classification/83778/1 "2007-01-29T18:08:26Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 29, 2007, 6:19pm UTC](https://www.ruby-forum.com/t/classification/83778/2 "2007-01-29T18:19:37Z")

</div>

Alle lunedì 29 gennaio 2007, Josselin ha scritto:

> 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

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 29, 2007, 7:26pm UTC](https://www.ruby-forum.com/t/classification/83778/3 "2007-01-29T19:26:05Z")

</div>

On 2007-01-29 18:18:31 +0100, Stefano C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
said:

> > tag = Tag.new(product.title, product.price)
> 
> should replace the line  
> )  
> result as your code.
> 
> I hope this helps
> 
> Stefano

thansk a lot … it’s so DRY…

btw : I tried also

```
    box = []
    0.upto(4) do |i|
      box[i] = []
    end

```

---

<div class="post-metadata">

**Author:** ![Josselin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@Josselin](https://www.ruby-forum.com/u/Josselin)\
**Post date:** [January 29, 2007, 7:51pm UTC](https://www.ruby-forum.com/t/classification/83778/4 "2007-01-29T19:51:13Z")

</div>

On 29.01.2007 19:20, Josselin wrote:

> > > box[0] = box[1] = box[2] = box[3] = []  
> > > tfyl
> > 
> > box=Array.new(4){[]}  
> > Stefano
> 
> thansk a lot … it’s so DRY…
> 
> btw : I tried also
> 
> ```
> box = []
> 0.upto(4) do |i|
> box[i] = []
> end
> 
> ```

Much more convenient is this:

box = Hash.new {|h,k| h[k] = []}

Now you can just do

for product in products  
tag = Tag.new(product.title, product.price)  
box[product.code] \<\< tag  
end

Kind regards

robert
