Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Newbiest ? ever

Reply
Thread Tools

Newbiest ? ever

 
 
Orion Hunter
Guest
Posts: n/a
 
      02-09-2004
(ruby 1.8.0 Linux)

I don't understand why I get the following behavriour:

irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
irb(main):002:0> matrix[0][0] = 1
=> 1
irb(main):003:0> matrix
=> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
irb(main):004:0>

I would have expected the assignment of matrix[0][0] to produce a matrix:
[[1,0,0,0],[0,0,0,0][0,0,0,0]]

What am I not getting here?

__________________________________________________ _______________
Plan your next US getaway to one of the super destinations here.
http://special.msn.com/local/hotdestinations.armx



 
Reply With Quote
 
 
 
 
Kent S.
Guest
Posts: n/a
 
      02-09-2004
Because, matrix = Array.new( 3, Array.new( 4, 0 ) )
creates an array with three elements all poining to the same array
object. What you need is something like that:

matrix = Array.new( 3 ) { Array.new( 4, 0 ) }

/kent

Orion Hunter wrote:
> (ruby 1.8.0 Linux)
>
> I don't understand why I get the following behavriour:
>
> irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
> => [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
> irb(main):002:0> matrix[0][0] = 1
> => 1
> irb(main):003:0> matrix
> => [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
> irb(main):004:0>
>
> I would have expected the assignment of matrix[0][0] to produce a matrix:
> [[1,0,0,0],[0,0,0,0][0,0,0,0]]
>
> What am I not getting here?
>
> __________________________________________________ _______________
> Plan your next US getaway to one of the super destinations here.
> http://special.msn.com/local/hotdestinations.armx
>
>
>

 
Reply With Quote
 
 
 
 
gabriele renzi
Guest
Posts: n/a
 
      02-09-2004
il Tue, 10 Feb 2004 02:56:28 +0900, "Orion Hunter"
<(E-Mail Removed)> ha scritto::

>(ruby 1.8.0 Linux)
>
>I don't understand why I get the following behavriour:
>
>irb(main):001:0> matrix = Array.new( 3, Array.new( 4, 0 ) )
>=> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]
>irb(main):002:0> matrix[0][0] = 1
>=> 1
>irb(main):003:0> matrix
>=> [[1, 0, 0, 0], [1, 0, 0, 0], [1, 0, 0, 0]]
>irb(main):004:0>
>
>I would have expected the assignment of matrix[0][0] to produce a matrix:
>[[1,0,0,0],[0,0,0,0][0,0,0,0]]
>
>What am I not getting here?


Array.new(num,object) just creates num refernces to object:
irb(main):001:0> a=Array.new(3,'yo')
=> ["yo", "yo", "yo"]
irb(main):002:0> a[0].id
=> 20896272
irb(main):003:0> a[1].id
=> 20896272

what you want is different object with the same value, you can do this
with:
irb(main):004:0> a=Array.new(3) {'yo'}
=> ["yo", "yo", "yo"]
irb(main):005:0> a[0].id
=> 20863668
irb(main):006:0> a[1].id
=> 20863656

so you should write:

matrix= Array.new(3) do
Array.new(4,0)
end

or just do:
require 'matrix'



 
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
Best list of free software you'll ever see! Goofy Software 46 06-25-2009 08:24 AM
Problems ever since SP2 =?Utf-8?B?QmVu?= Wireless Networking 1 12-12-2004 06:47 PM
The ever decreasing circles syndrome & wireless networking! David W Allen Wireless Networking 9 11-18-2004 11:57 AM
Will WPS ever support other EAP methods? =?Utf-8?B?VG9tIFJpeG9t?= Wireless Networking 0 08-23-2004 11:39 AM
don't you ever, don't you ever, lower yourself, forgetting all your standards Jason ASP .Net Web Controls 0 07-06-2004 12:50 PM



Advertisments