Sha256: 8cc9aeafbe67377f2fd5cf90be1d9a29aca2da0a818e65efeb2ca9dbfc500083

Contents?: true

Size: 821 Bytes

Versions: 6

Compression:

Stored size: 821 Bytes

Contents

# AutoArray
# Copyright (c) 2005 Brian Schröder

# An Array that automatically expands dimensions as needed.
#
#   a  = Autoarray.new
#   a[1][2][3] = 12
#   a             #=> [nil, [nil, nil, [nil, nil, nil, 12]]]
#   a[2][3][4]    #=> []
#   a             #=> [nil, [nil, nil, [nil, nil, nil, 12]]]
#   a[1][-2][1] = "Negative"
#   a             #=> [nil, [nil, [nil, "Negative"], [nil, nil, nil, 12]]]
#
class AutoArray < Array

  def initialize(size=0, default=nil, update = nil, update_index = nil)
    super(size, default)
    @update, @update_index = update, update_index
  end

  def [](k)
    if -self.length+1 < k and k < self.length
      super(k)
    else
      Autoarray.new(0, nil, self, k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
facets-2.8.4 lib/more/facets/autoarray.rb
facets-2.8.3 lib/more/facets/autoarray.rb
facets-2.8.2 lib/more/facets/autoarray.rb
facets-2.8.1 lib/more/facets/autoarray.rb
facets-2.8.0 lib/more/facets/autoarray.rb
facets-2.7.0 lib/more/facets/autoarray.rb