[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Object Model bothering me

Kurt M. Dresner

9/13/2003 11:48:00 PM

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

5 Answers

Dan Doel

9/14/2003 12:11:00 AM

0

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


Robert Feldt

9/14/2003 12:26:00 AM

0

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]

? ;)

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


Christoph R.

9/14/2003 1:47:00 AM

0

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

Kurt M. Dresner

9/14/2003 2:32:00 AM

0

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]
>
> ? ;)
>
> 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 =======<

Martin DeMello

9/14/2003 5:54:00 AM

0

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.

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