Class: KXI::Collections::ArrayCollection

Inherits:
Enumerable
  • Object
show all
Defined in:
lib/kxi/collections/array_collection.rb

Overview

Makes array enumerable

Defined Under Namespace

Classes: ArrayEnumerator

Instance Method Summary collapse

Methods inherited from Enumerable

#aggregate, #all, #any, #can_alter?, #count, #enumerator, #first, #first!, #foreach, #last, #last!, #max, #min, #of_type, #of_type!, #order_by, #order_by_descending, #select, #select_many, #skip, #take, #to_array, #where

Constructor Details

#initialize(array = []) ⇒ ArrayCollection

Instantiates the KXI::Collections::ArrayCollection class

Parameters:

  • array (Array) (defaults to: [])

    Array for enumeration



9
10
11
12
# File 'lib/kxi/collections/array_collection.rb', line 9

def initialize(array = [])
	super()
	@arr = array
end

Instance Method Details

#[](index) ⇒ Object

Obtains value in array at specific index

Parameters:

  • index (Number)

    Index in array to obtain

Returns:

  • (Object)

    Value in array at specified index

Raises:



24
25
26
27
28
29
30
# File 'lib/kxi/collections/array_collection.rb', line 24

def [](index)
	raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
	raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
	lock do
		return @arr[index]
	end
end

#[]=(index, value) ⇒ Object

Sets value of array at specific index

Parameters:

  • index (Number)

    Index in array to set

  • value (Object)

    Value to set

Returns:

  • (Object)

    Set value

Raises:



38
39
40
41
42
43
44
45
# File 'lib/kxi/collections/array_collection.rb', line 38

def []=(index, value)
	raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
	raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
	lock(true) do
		@arr[index] = value
	end
	return value
end

#add(value) ⇒ Number

Adds value into array

Parameters:

  • value (Object)

    Value to set

Returns:

  • (Number)

    Assigned index

Raises:



51
52
53
54
55
56
57
# File 'lib/kxi/collections/array_collection.rb', line 51

def add(value)
	idx = @arr.length
	lock(true) do
		@arr.push(value)
	end
	return idx
end

#remove_at(index) ⇒ Object

Removes item at specific index

Parameters:

  • index (Number)

    Index to remove

Returns:

  • (Object)

    Removed item

Raises:



64
65
66
67
68
69
70
71
72
# File 'lib/kxi/collections/array_collection.rb', line 64

def remove_at(index)
	raise(KXI::Exceptions::OutOfRangeException.new(index)) if @arr.length == 0
	raise(KXI::Exceptions::OutOfRangeException.new(index, 0, @arr.length - 1)) if index < 0 or index >= @arr.length
	ret = nil
	lock(true) do
		ret = @arr.delete_at(index)
	end
	return ret
end