Class: Autoarray

Inherits:
Array show all
Defined in:
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

#at_rand, #at_rand!, cast_from, #conjoin, #delete_unless, #delete_values, #delete_values_at, #each_iteration, #merge!, mutable_methods, #not_empty?, #only, #pad, #pad!, #pick, #pick!, #rand_index, #rand_subset, #restore_snapshot, #rotate, #rotate!, #select!, #shuffle, #shuffle!, #splice, #take_snapshot, #to_b, #to_console, #to_h, #to_path, #to_t

Methods included from Stackable

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

Methods included from Indexable

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

Constructor Details

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

Returns a new instance of Autoarray.



41
42
43
44
# File 'lib/more/facets/autoarray.rb', line 41

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



46
47
48
49
50
51
52
# File 'lib/more/facets/autoarray.rb', line 46

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



54
55
56
57
# File 'lib/more/facets/autoarray.rb', line 54

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