# Ruby Object Model bothering me

**URL:** <https://rubytalk.org/t/ruby-object-model-bothering-me/8032>\
**Category:** ruby-talk\
**Created:** [13 September 2003 23:48 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032 "2003-09-13T23:48:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [13 September 2003 23:48 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/1 "2003-09-13T23:48:17Z")

</div>

Ok, so I did

a = Array.new(number, [])

It’s really bothering me that if I do a[i] = n, all of a is now n.

Is there a graceful way to get around this?

-Kurt

---

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [14 September 2003 00:11 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/2 "2003-09-14T00:11:16Z")

</div>

Kurt M. Dresner wrote:

> Ok, so I did
> 
> a = Array.new(number, )
> 
> It’s really bothering me that if I do a[i] = n, all of a is now n.
> 
> Is there a graceful way to get around this?

When I do:

a = Array.new(6, )

a[5] = 3

p a[2]

I get printed out.

- Dan

---

<div class="post-metadata">

**Author:** ![Christoph\_R](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@Christoph\_R](https://rubytalk.org/u/Christoph_R)\
**Post date:** [14 September 2003 02:02 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/3 "2003-09-14T02:02:06Z")

</div>

Kurt M. Dresner wrote:

> Ok, so I did
> 
> a = Array.new(number, )
> 
> It’s really bothering me that if I do a[i] = n, all of a is now n.
> 
> Is there a graceful way to get around this?

I guess you want

> **···**
>
> * * *
> 
> a = Array.new(6){}  
> a[3][1] = 8
> 
> ## p a[3] # [nil,8] p a[4] #
> 
> /Christoph

---

<div class="post-metadata">

**Author:** ![John\_Long2](https://avatars.discourse-cdn.com/v4/letter/j/e9c0ed/32.png) [@John\_Long2](https://rubytalk.org/u/John_Long2)\
**Post date:** [14 September 2003 02:49 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/4 "2003-09-14T02:49:07Z")

</div>

Hi Kurt,

> Ok, so I did
> 
> a = Array.new(number, )
> 
> It’s really bothering me that if I do a[i] = n, all of a is now n.
> 
> Is there a graceful way to get around this?

I’m not sure what you are trying to do here. Maybe you could give a more  
detailed description of your problem?

If you are trying to create a new array you have a couple of options.

The default way to construct an array:  
a = [1,2,3]

If you want to create an empty array that is a certain size:  
a = Array.new(5) #=\> [nil, nil, nil, nil, nil]

If you want to create an array that is a certain size and all the elements  
have the same value:  
a = Array.new(3, “pickles”) #=\> [“pickles”, “pickles”, “pickles”]

> **···**
>
> –  
> John Long

---

<div class="post-metadata">

**Author:** ![Chalmers](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Chalmers](https://rubytalk.org/u/Chalmers)\
**Post date:** [14 September 2003 00:25 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/5 "2003-09-14T00:25:37Z")

</div>

Dan Doel [djd15@po.cwru.edu](mailto:djd15@po.cwru.edu) skrev den Sun, 14 Sep 2003 09:11:16 +0900:

> Kurt M. Dresner wrote:
> 
> > Ok, so I did
> > 
> > a = Array.new(number, )
> > 
> > It’s really bothering me that if I do a[i] = n, all of a is now n.
> > 
> > Is there a graceful way to get around this?
> 
> When I do:
> 
> a = Array.new(6, )
> 
> a[5] = 3
> 
> p a[2]
> 
> I get printed out.

Yeah, but what about

a = Array.new(6, )  
a[5] \<\< 3  
p a[2]

? 😉

I think the original poster might wanna try

a = Array.new(6) {Array.new}

but I’m not sure I understand what he wants…

Regards,

Robert

> **···**
>
> > - Dan
> 
> –  
> Robert Feldt

---

<div class="post-metadata">

**Author:** ![Kurt\_M\_Dresner](https://avatars.discourse-cdn.com/v4/letter/k/c57346/32.png) [@Kurt\_M\_Dresner](https://rubytalk.org/u/Kurt_M_Dresner)\
**Post date:** [14 September 2003 02:31 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/6 "2003-09-14T02:31:47Z")

</div>

Yeah, that’s what I meant. When I do a[i].push(foo) then all of a is  
foo. I forgot about the block for the constructor that gets run each  
time. Now I am a happy rubyist once again.

-Kurt

> **···**
>
> On Sun, Sep 14, 2003 at 09:25:37AM +0900, Robert Feldt wrote:
> 
> > Dan Doel [djd15@po.cwru.edu](mailto:djd15@po.cwru.edu) skrev den Sun, 14 Sep 2003 09:11:16 +0900:
> > 
> > > Kurt M. Dresner wrote:
> > > 
> > > > Ok, so I did
> > > > 
> > > > a = Array.new(number, )
> > > > 
> > > > It’s really bothering me that if I do a[i] = n, all of a is now n.
> > > > 
> > > > Is there a graceful way to get around this?
> > > 
> > > When I do:
> > > 
> > > a = Array.new(6, )
> > > 
> > > a[5] = 3
> > > 
> > > p a[2]
> > > 
> > > I get printed out.
> > 
> > Yeah, but what about
> > 
> > a = Array.new(6, )  
> > a[5] \<\< 3  
> > p a[2]
> > 
> > ? 😉
> > 
> > I think the original poster might wanna try
> > 
> > a = Array.new(6) {Array.new}
> > 
> > but I’m not sure I understand what he wants…
> > 
> > Regards,
> > 
> > Robert
> > 
> > > - Dan
> > 
> > –  
> > Robert Feldt
> > 
> > ======= End of Original Message =======\<

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [14 September 2003 06:02 UTC](https://rubytalk.org/t/ruby-object-model-bothering-me/8032/7 "2003-09-14T06:02:31Z")

</div>

Just for completeness, the problem with Array.new(6, ) isn’t Ruby’s  
object model - it’s that Array.new(num, object) stores a reference to  
the object passed in in each of its cells, not a copy of it. There’s no  
ruby-decreed reason it couldn’t have been written to call object.dup  
each time it filled a new cell.

martin

> **···**
>
> Kurt M. Dresner [kdresner@cs.utexas.edu](mailto:kdresner@cs.utexas.edu) wrote:
> 
> > Yeah, that’s what I meant. When I do a[i].push(foo) then all of a is  
> > foo. I forgot about the block for the constructor that gets run each  
> > time. Now I am a happy rubyist once again.
