Professional Web Applications Themes

Newbiest ? ever - Ruby

(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...

  1. #1

    Default Newbiest ? ever

    (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



    Orion Guest

  2. #2

    Default Re: Newbiest ? ever

    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: 
    Kent Guest

  3. #3

    Default Re: Newbiest ? ever

    il Tue, 10 Feb 2004 02:56:28 +0900, "Orion Hunter"
    <com> ha scritto::
     

    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'
    :)


    gabriele Guest

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139