Class: Daru::Vector
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#vector ⇒ Object
readonly
Returns the value of attribute vector.
Instance Method Summary collapse
- #<<(element) ⇒ Object
- #==(other) ⇒ Object
- #[](index) ⇒ Object
- #[]=(index, value) ⇒ Object
- #compact! ⇒ Object
- #daru_vector ⇒ Object (also: #dv)
- #delete(index) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #first(lim = 1) ⇒ Object
-
#initialize(source = [], name = nil) ⇒ Vector
constructor
A new instance of Vector.
- #to_a ⇒ Object
- #to_html(threshold = 15) ⇒ Object
- #to_json ⇒ Object
- #to_nmatrix ⇒ Object (also: #to_nm)
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
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/daru/vector.rb', line 9 def name @name end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
11 12 13 |
# File 'lib/daru/vector.rb', line 11 def size @size end |
#vector ⇒ Object (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_vector ⇒ Object 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 |
#dup ⇒ Object
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_a ⇒ Object
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_json ⇒ Object
51 52 53 |
# File 'lib/daru/vector.rb', line 51 def to_json self.to_a.to_json end |
#to_nmatrix ⇒ Object Also known as: to_nm
59 60 61 |
# File 'lib/daru/vector.rb', line 59 def to_nmatrix @vector.to_nm end |