Ruby multidimensional array

前端 未结 11 1356
春和景丽
春和景丽 2020-11-28 09:30

Maybe it\'s just my lack of abilities to find stuff here that is the problem, but I can\'t find anything about how to create multidimensional arrays in Ruby.

Could s

相关标签:
11条回答
  • 2020-11-28 09:31

    I had to reproduce PHP-style multidimensional array in Ruby recently. Here is what I did:

    # Produce PHP-style multidimensional array.
    #
    # Example
    #
    # arr = Marray.new
    #
    # arr[1][2][3] = "foo"
    # => "foo"
    #
    # arr[1][2][3]
    # => "foo"
    
    class Marray < Array
      def [](i)
        super.nil? ? self[i] = Marray.new : super
      end
    end
    
    0 讨论(0)
  • 2020-11-28 09:34

    Actually this is much quicker than the block method given above:

    arr = Array.new(n, Array.new(n, Array.new(n,0.0))) 
    
    arr[0][1][2] += 1
    
    0 讨论(0)
  • 2020-11-28 09:40

    The method given above don't works.

    n = 10
    arr = Array.new(n, Array.new(n, Array.new(n,0.0))) 
    arr[0][1][2] += 1
    puts arr[0][2][2]
    

    is equivalent to

    n = 10
    a = Array.new(n,0.0)
    b = Array.new(n,a)
    arr = Array.new(n, b) 
    arr[0][1][2] += 1
    puts arr[0][2][2]
    

    and will print 1.0, not 0.0, because we are modifiyng array a and printing the element of array a.

    0 讨论(0)
  • 2020-11-28 09:41

    Strictly speaking it is not possible to create multi dimensional arrays in Ruby. But it is possible to put an array in another array, which is almost the same as a multi dimensional array.

    This is how you could create a 2D array in Ruby:

    a = [[1,2,3], [4,5,6], [7,8,9]]
    


    As stated in the comments, you could also use NArray which is a Ruby numerical array library:

    require 'narray'
    b = NArray[ [1,2,3], [4,5,6], [7,8,9] ]
    

    Use a[i][j] to access the elements of the array. Basically a[i] returns the 'sub array' stored on position i of a and thus a[i][j] returns element number j from the array that is stored on position i.

    0 讨论(0)
  • 2020-11-28 09:46

    It might help to remember that the array is an object in ruby, and objects are not (by default) created simply by naming them or naming a the object reference. Here is a routine for creating a 3 dimension array and dumping it to the screen for verification:

    def Create3DimensionArray(x, y, z, default)
        n = 0                       # verification code only
        ar = Array.new(x)
        for i in 0...x
            ar[i] = Array.new(y)
            for j in 0...y
                ar[i][j] = Array.new(z, default)
                for k in 0...z      # verification code only
                    ar[i][j][k] = n # verification code only
                    n += 1          # verification code only
                end                 # verification code only
            end
        end
        return ar
    end
    
    # Create sample and verify
    ar = Create3DimensionArray(3, 7, 10, 0)
    
    for x in ar
        puts "||"
        for y in x
            puts "|"
            for z in y
                printf "%d ", z
            end
        end
    end
    0 讨论(0)
  • 2020-11-28 09:51

    you can pass a block to Array.new

    Array.new(n) {Array.new(n,default_value)}
    

    the value that returns the block will be the value of each index of the first array,

    so..

    Array.new(2) {Array.new(2,5)} #=> [[5,5],[5,5]]
    

    and you can access this array using array[x][y]

    also for second Array instantiation, you can pass a block as default value too. so

    Array.new(2) { Array.new(3) { |index| index ** 2} } #=> [[0, 1, 4], [0, 1, 4]]
    
    0 讨论(0)
提交回复
热议问题