Class: Autoarray

Inherits:
Array show all
Defined in:
lib/gems/facets-2.4.5/lib/more/facets/autoarray.rb

Overview

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]]]

Instance Method Summary collapse

Methods inherited from Array

#_facets_index, cast_from, #combination, #conjoin, #count, #delete_unless, #delete_values, #delete_values_at, #each_iteration, #include?, #index, #invert, #merge!, mutable_methods, #not_empty?, #only, #pad, #pad!, #parse_splat_args, #permutation, #pick_random, #power_set, #product, #random_each, #randomize, #randomize!, #recursively, #recursively!, #restore_snapshot, #rotate, #rotate!, #select!, #splice, #subset?, #superset?, #take_snapshot, #to_b, #to_h, #to_shell, #to_shellwords, #to_t, #traverse, #traverse!

Methods included from Stackable

#peek, #poke, #pop, #pull, #push

Methods included from Indexable

#body, #ends, #first, #first!, #first=, #foot, #head, #index, #last, #last!, #last=, #mid, #middle, #pos, #range, #tail, #thru

Methods included from Random

append_features

Methods included from English::Array

#conjunction

Constructor Details

#initialize(size = 0, default = nil, update = nil, update_index = nil) ⇒ Autoarray

Returns a new instance of Autoarray.



44
45
46
47
# File 'lib/gems/facets-2.4.5/lib/more/facets/autoarray.rb', line 44

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

Instance Method Details

#[](k) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/gems/facets-2.4.5/lib/more/facets/autoarray.rb', line 49

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

#[]=(k, v) ⇒ Object



57
58
59
60
# File 'lib/gems/facets-2.4.5/lib/more/facets/autoarray.rb', line 57

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