Class: Daru::Vector

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/daru/vector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source = [], name = nil) ⇒ Vector

Returns a new instance of Vector.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/daru/vector.rb', line 15

def initialize source=[], name=nil
  if source.is_a?(Hash)
    initialize source.values[0], source.keys[0]
  else
    @name = name || SecureRandom.uuid

    @vector = 
    case source
    when Range, Matrix
      source.to_a.flatten
    else
      source
    end

    @size = @vector.size
  end
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



9
10
11
# File 'lib/daru/vector.rb', line 9

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



11
12
13
# File 'lib/daru/vector.rb', line 11

def size
  @size
end

#vectorObject (readonly)

Returns the value of attribute vector.



13
14
15
# File 'lib/daru/vector.rb', line 13

def vector
  @vector
end

Instance Method Details

#<<(element) ⇒ Object



45
46
47
48
49
# File 'lib/daru/vector.rb', line 45

def <<(element)
  @vector << element

  @size += 1
end

#==(other) ⇒ Object



41
42
43
# File 'lib/daru/vector.rb', line 41

def ==(other)
  other.vector == @vector and other.name == @name and other.size == @size
end

#[](index) ⇒ Object



33
34
35
# File 'lib/daru/vector.rb', line 33

def [](index)
  @vector[index]
end

#[]=(index, value) ⇒ Object



37
38
39
# File 'lib/daru/vector.rb', line 37

def []=(index, value)
  @vector[index] = value
end

#compact!Object



97
98
99
# File 'lib/daru/vector.rb', line 97

def compact!
  @vector.compact!
end

#daru_vectorObject Also known as: dv



91
92
93
# File 'lib/daru/vector.rb', line 91

def daru_vector
  self
end

#delete(index) ⇒ Object



69
70
71
72
73
# File 'lib/daru/vector.rb', line 69

def delete index
  @vector[index] = nil
  @vector.compact!
  @size -= 1
end

#dupObject



87
88
89
# File 'lib/daru/vector.rb', line 87

def dup
  Daru::Vector.new @vector.dup, @name
end

#each(&block) ⇒ Object



5
6
7
# File 'lib/daru/vector.rb', line 5

def each(&block)
  @vector.each(&block)
end

#first(lim = 1) ⇒ Object



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

def first lim=1
  lim == 1 ? @vector.first : @vector.first(lim)
end

#to_aObject



55
56
57
# File 'lib/daru/vector.rb', line 55

def to_a
  @vector.to_a
end

#to_html(threshold = 15) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/daru/vector.rb', line 75

def to_html threshold=15
  html = '<table><tr><th>' + @name.to_s + '</th></tr>>'

  @vector.to_a.each_with_index do |el,i|
    next if threshold < i and i < @arr.length-1
    content = i == threshold ? '...' : el.to_s
    html.concat('<tr><td>' + content  + '</td></tr>')
  end

  html += '</table>'
end

#to_jsonObject



51
52
53
# File 'lib/daru/vector.rb', line 51

def to_json
  self.to_a.to_json
end

#to_nmatrixObject Also known as: to_nm



59
60
61
# File 'lib/daru/vector.rb', line 59

def to_nmatrix
  @vector.to_nm
end