Create two-dimensional arrays and access sub-arrays in Ruby

前端 未结 9 1004
走了就别回头了
走了就别回头了 2020-11-28 03:26

I wonder if there\'s a possibility to create a two dimensional array and to quickly access any horizontal or vertical sub array in it?

I believe we can access a hor

9条回答
  •  悲哀的现实
    2020-11-28 04:07

    You didn't state your actual goal, but maybe this can help:

    require 'matrix'  # bundled with Ruby
    m = Matrix[
     [1, 2, 3],
     [4, 5, 6]
    ]
    
    m.column(0) # ==> Vector[1, 4]
    

    (and Vectors acts like arrays)

    or, using a similar notation as you desire:

    m.minor(0..1, 2..2) # => Matrix[[3], [6]]
    

提交回复
热议问题