Ruby Object Model bothering me

K

Kurt M. Dresner

Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = n, all of a is now n.

Is there a graceful way to get around this?

-Kurt
 
D

Dan Doel

Kurt said:
Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = 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
 
R

Robert Feldt

Dan Doel said:
Kurt said:
Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = 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
 
C

Christoph R.

Kurt said:
Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = 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] # []
 
K

Kurt M. Dresner

Yeah, that's what I meant. When I do a.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

Dan Doel said:
Kurt said:
Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = 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



--
Robert Feldt


======= End of Original Message =======<
 
J

John W. Long

Hi Kurt,
Ok, so I did

a = Array.new(number, [])

It's really bothering me that if I do a = 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"]
 
M

Martin DeMello

Kurt M. Dresner said:
Yeah, that's what I meant. When I do a.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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top