Sha256: 2f255d66d898cf154e6c25a6e63b6506671a38d989791aeb09a290e82763d1bb

Contents?: true

Size: 1.55 KB

Versions: 11

Compression:

Stored size: 1.55 KB

Contents

# = AutoArray
#
# 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]]]
#
# == Authors
#
# * Brian Schröder
#
# == License
#
#   Copyright (c) 2005 Brian Schröder
#
#   Ruby License
#
#   This module is free software. You may use, modify, and/or redistribute this
#   software under the same terms as Ruby.
#
#   This program is distributed in the hope that it will be useful, but WITHOUT
#   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
#   FOR A PARTICULAR PURPOSE.

# = AutoArray
#
# 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

11 entries across 11 versions & 2 rubygems

Version Path
facets-2.6.0 lib/more/facets/autoarray.rb
facets-2.4.0 lib/facets/autoarray.rb
facets-2.4.1 lib/facets/autoarray.rb
facets-2.4.3 lib/more/facets/autoarray.rb
facets-2.4.2 lib/more/facets/autoarray.rb
facets-2.4.4 lib/more/facets/autoarray.rb
facets-2.5.0 lib/more/facets/autoarray.rb
facets-2.4.5 lib/more/facets/autoarray.rb
facets-2.5.1 lib/more/facets/autoarray.rb
facets-2.5.2 lib/more/facets/autoarray.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/more/facets/autoarray.rb