Class: Primitive::Array

Inherits:
Object
  • Object
show all
Defined in:
lib/primitive_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(length) ⇒ Array

Returns a new instance of Array.



4
5
6
7
# File 'lib/primitive_array.rb', line 4

def initialize(length)
  @length = length
  @storage = []
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



3
4
5
# File 'lib/primitive_array.rb', line 3

def length
  @length
end

Instance Method Details

#[](index) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
# File 'lib/primitive_array.rb', line 14

def [](index)
  raise ArgumentError, 'Attempted to access an out-of-bounds element' unless index < @length
  @storage[index]
end

#[]=(index, element) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/primitive_array.rb', line 9

def []=(index, element)
  raise ArgumentError, 'Attempted to access an out-of-bounds element' unless index < @length
  @storage[index] = element
end