Class: Daru::Accessors::ArrayWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/daru/accessors/array_wrapper.rb

Overview

Internal class for wrapping ruby array

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vector, context) ⇒ ArrayWrapper

Returns a new instance of ArrayWrapper.



23
24
25
26
27
28
# File 'lib/daru/accessors/array_wrapper.rb', line 23

def initialize vector, context
  @data = vector.to_a
  @context = context

  set_size
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



21
22
23
# File 'lib/daru/accessors/array_wrapper.rb', line 21

def data
  @data
end

#sizeObject

Returns the value of attribute size.



20
21
22
# File 'lib/daru/accessors/array_wrapper.rb', line 20

def size
  @size
end

Instance Method Details

#<<(element) ⇒ Object



52
53
54
55
# File 'lib/daru/accessors/array_wrapper.rb', line 52

def << element
  @data << element
  set_size
end

#==(other) ⇒ Object



39
40
41
# File 'lib/daru/accessors/array_wrapper.rb', line 39

def == other
  @data == other
end

#[](*index) ⇒ Object



30
31
32
# File 'lib/daru/accessors/array_wrapper.rb', line 30

def [] *index
  @data[*index]
end

#[]=(index, value) ⇒ Object



34
35
36
37
# File 'lib/daru/accessors/array_wrapper.rb', line 34

def []= index, value
  @data[index] = value
  set_size
end

#delete_at(index) ⇒ Object



43
44
45
46
# File 'lib/daru/accessors/array_wrapper.rb', line 43

def delete_at index
  @data.delete_at index
  set_size
end

#dupObject



65
66
67
# File 'lib/daru/accessors/array_wrapper.rb', line 65

def dup
  ArrayWrapper.new @data.dup, @context
end

#each(&block) ⇒ Object



10
11
12
13
# File 'lib/daru/accessors/array_wrapper.rb', line 10

def each(&block)
  @data.each(&block)
  self
end

#index(key) ⇒ Object



48
49
50
# File 'lib/daru/accessors/array_wrapper.rb', line 48

def index key
  @data.index key
end

#map!(&block) ⇒ Object



15
16
17
18
# File 'lib/daru/accessors/array_wrapper.rb', line 15

def map!(&block)
  @data.map!(&block)
  self
end

#maxObject



77
78
79
# File 'lib/daru/accessors/array_wrapper.rb', line 77

def max
  @data.max
end

#meanObject



69
70
71
# File 'lib/daru/accessors/array_wrapper.rb', line 69

def mean
  sum.quo(@size - @context.missing_positions.size).to_f
end

#minObject



81
82
83
# File 'lib/daru/accessors/array_wrapper.rb', line 81

def min
  @data.min
end

#productObject



73
74
75
# File 'lib/daru/accessors/array_wrapper.rb', line 73

def product
  @data.inject(1) { |m,e| m*e unless e.nil? }
end

#sumObject



85
86
87
88
89
90
# File 'lib/daru/accessors/array_wrapper.rb', line 85

def sum
  @data.inject(0) do |memo ,e|
    memo += e unless e.nil? #TODO: Remove this conditional somehow!
    memo
  end
end

#to_aObject



61
62
63
# File 'lib/daru/accessors/array_wrapper.rb', line 61

def to_a
  @data
end

#uniqObject



57
58
59
# File 'lib/daru/accessors/array_wrapper.rb', line 57

def uniq
  @data.uniq
end