Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > has any one tried this?

Reply
Thread Tools

has any one tried this?

 
 
Wu Ning
Guest
Posts: n/a
 
      01-03-2008
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-forum.com/.

 
Reply With Quote
 
 
 
 
Frederick Cheung
Guest
Posts: n/a
 
      01-03-2008

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-forum.com/.
>



 
Reply With Quote
 
 
 
 
Daniel Lucraft
Guest
Posts: n/a
 
      01-03-2008
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-forum.com/.

 
Reply With Quote
 
Casimir
Guest
Posts: n/a
 
      01-03-2008
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
 
Reply With Quote
 
Wu Ning
Guest
Posts: n/a
 
      01-03-2008

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-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      01-03-2008
On Jan 3, 2008 1:11 PM, Wu Ning <(E-Mail Removed)> 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.blogspot.com/

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Has any one tried Sybex(MCAD/MCSD: Visual Basic .NET Windows and Web Applications Study Guide) book for 70-306-70-305? Arash MCAD 0 12-12-2004 06:25 AM
any one tried the DSE USB 2.0 video capture adaptor? Malone NZ Computing 0 01-29-2004 08:47 PM
Zenon MagneFlash - Has anyone tried one of these ? Brian Computer Support 2 12-06-2003 03:37 AM
Has any tried the "Google Bar" yet? Allen Howell A+ Certification 2 09-24-2003 06:46 PM



Advertisments