Ruby multidimensional array

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

    Use can also use the XKeys Gem.

    require 'xkeys'
    
    regular = [].extend XKeys::Auto
    regular[1,2,3] = 4
    # [nil, [nil, nil, [nil, nil, nil, 4]]]
    regular[3,2,1, :else=>0] # 0
    # Note: regular[1,2] is a slice
    # Use regular[1,2,{}] to access regular[1][2]
    
    sparse = {}.extend XKeys::Hash
    sparse[1,2,3] = 4
    # {1=>{2=>{3=>4}}}
    path=[1,2,3]
    sparse[*path,{}] # 4
    

提交回复
热议问题