Class: DS::ExpandableArray
- Inherits:
-
Array
- Object
- Array
- DS::ExpandableArray
- Defined in:
- lib/ds/arrays/expandable_array.rb
Overview
Class provides access to any element in array. If index i is greter than array size then elements from size to i are filled with extender.
Instance Method Summary collapse
-
#[](x) ⇒ Object
Returns element at index.
-
#[]=(x, v) ⇒ Object
Sets the element at index.
-
#initialize(size = 0, extender = 0) ⇒ ExpandableArray
constructor
A new instance of ExpandableArray.
Constructor Details
#initialize(size = 0, extender = 0) ⇒ ExpandableArray
Returns a new instance of ExpandableArray.
6 7 8 9 |
# File 'lib/ds/arrays/expandable_array.rb', line 6 def initialize(size = 0, extender = 0) @extender = extender super(size, extender) end |
Instance Method Details
#[](x) ⇒ Object
Returns element at index. If index is greater than size of array then elements from size to index are filled with extender.
13 14 15 16 |
# File 'lib/ds/arrays/expandable_array.rb', line 13 def [](x) (size, x) if x >= size super(x) end |
#[]=(x, v) ⇒ Object
Sets the element at index. If index is greater than size of array then elements from size to index are filled with extender.
20 21 22 23 24 |
# File 'lib/ds/arrays/expandable_array.rb', line 20 def []=(x, v) old_size = size super(x, v) (old_size, size - 2) if size - old_size > 1 end |