Ruby Object Model bothering me

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

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

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

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

Dan Doel 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]

? :wink:

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

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 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]

? :wink:

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 =======<

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 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.