Ruby multidimensional array

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

提交回复
热议问题