[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

has any one tried this?

Wu Ning

1/3/2008 11:21:00 AM

My ruby version is 1.8.6
a = Array.new(3,Array.new())
a[2]<<1
puts a

the result of a is
[[1], [1], [1]]
in irb..

I don't understand why.
I just wannt insert into a 1 into the third array of a.
--
Posted via http://www.ruby-....

5 Answers

Frederick Cheung

1/3/2008 11:32:00 AM

0


On 3 Jan 2008, at 11:21, Wu Ning wrote:

> My ruby version is 1.8.6
> a = Array.new(3,Array.new())
> a[2]<<1
> puts a
>
> the result of a is
> [[1], [1], [1]]
> in irb..
>
> I don't understand why.
Array.new will insert the second argument you give into the array (3
times since that's what you've asked for). However it's the same array
(You can see this quite easily if you do Array.new(3,Array.new).map {|
x| x.object_id}). So a is not an array containing 3 arrays, it's an
array containing the same array 3 times.
a = Array.new(3) {[]}
should do the trick (since the block is called once for each element
of the array)

Fred


>
> I just wannt insert into a 1 into the third array of a.
> --
> Posted via http://www.ruby-....
>


Daniel Lucraft

1/3/2008 11:36:00 AM

0

Wu Ning wrote:
> My ruby version is 1.8.6
> a = Array.new(3,Array.new())
> a[2]<<1
> puts a
>
> the result of a is
> [[1], [1], [1]]
> in irb..
>
> I don't understand why.
> I just wannt insert into a 1 into the third array of a.

After a = Array.new(3,Array.new()), a is an array containing three
references to the same object. There is only one array, but it is
repeated three times.

Do
a = []; 3.times { a << [] }
and you'll have three different arrays.

best,
Dan
--
Posted via http://www.ruby-....

Casimir

1/3/2008 11:49:00 AM

0

Wu Ning kirjoitti:
> My ruby version is 1.8.6
> a = Array.new(3,Array.new())
> a[2]<<1
> puts a

I suppose you wanted to type

irb(main):012:0> uber = [[], [], 3]
=> [[], [], 3]


Csmr

Wu Ning

1/3/2008 12:11:00 PM

0


Thanks,
I know what's going on now,
:)

Daniel Lucraft wrote:
> Wu Ning wrote:
>> My ruby version is 1.8.6
>> a = Array.new(3,Array.new())
>> a[2]<<1
>> puts a
>>
>> the result of a is
>> [[1], [1], [1]]
>> in irb..
>>
>> I don't understand why.
>> I just wannt insert into a 1 into the third array of a.
>
> After a = Array.new(3,Array.new()), a is an array containing three
> references to the same object. There is only one array, but it is
> repeated three times.
>
> Do
> a = []; 3.times { a << [] }
> and you'll have three different arrays.
>
> best,
> Dan

--
Posted via http://www.ruby-....

Robert Dober

1/3/2008 5:26:00 PM

0

On Jan 3, 2008 1:11 PM, Wu Ning <ngloom@gmail.com> wrote:
>
> Thanks,
> I know what's going on now,
> :)
Good ;) Ruby has however a syntax that will do what you intended in
the first place, see below please... and yes err
please do not top post unless you have a very good reason to do so in
which case I apologize for having mentioned it.
>
>
> Daniel Lucraft wrote:
> > Wu Ning wrote:
> >> My ruby version is 1.8.6
> >> a = Array.new(3,Array.new())

a = Array.new(3){ [] }

> >> a[2]<<1
> >> puts a

[ [],[],[1] ]

HTH
Robert

--
http://ruby-smalltalk.blo...

---
Whereof one cannot speak, thereof one must be silent.
Ludwig Wittgenstein