Ruby multidimensional array

前端 未结 11 1477
春和景丽
春和景丽 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:57

    Just a clarification:

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

    is not at all the same as:

    arr = Array.new(2, Array.new(2, 5))
    

    in the later case, try:

    arr[0][0] = 99
    

    and this is what you got:

    [[99,5], [99,5]]
    

提交回复
热议问题