Class: TTY::Vector
- Inherits:
-
Object
- Object
- TTY::Vector
- Includes:
- Enumerable, Conversion, Equatable
- Defined in:
- lib/tty/vector.rb
Overview
This class represents a mathematical vector.
Direct Known Subclasses
Instance Attribute Summary
Attributes included from Equatable
Class Method Summary collapse
-
.[](*array) ⇒ Vector
Utility method to instantiate a Vector.
Instance Method Summary collapse
-
#[](indx) ⇒ Object
(also: #at, #element)
Return element at index.
-
#[]=(indx, value) ⇒ Object
(also: #set_element)
Set a value of the element for the given index.
-
#each ⇒ self
Iterate over each element in the vector.
-
#empty? ⇒ Boolean
Check if there are not elements.
-
#initialize(array = []) ⇒ undefined
constructor
Instantiate a Vector.
-
#size ⇒ Integer
(also: #length)
Check number of elements.
-
#to_a ⇒ Array
Return the vector elements in an array.
-
#to_ary ⇒ Object
Convert to array.
Methods included from Equatable
#attr_reader, included, #inherited
Methods included from Conversion
Constructor Details
#initialize(array = []) ⇒ undefined
Instantiate a Vector
29 30 31 |
# File 'lib/tty/vector.rb', line 29 def initialize(array = []) @elements = convert_to_array(array) end |
Class Method Details
.[](*array) ⇒ Vector
Utility method to instantiate a Vector
18 19 20 |
# File 'lib/tty/vector.rb', line 18 def self.[](*array) new convert_to_array(array) end |
Instance Method Details
#[](indx) ⇒ Object Also known as: at, element
Return element at index.
42 43 44 |
# File 'lib/tty/vector.rb', line 42 def [](indx) elements[indx] end |
#[]=(indx, value) ⇒ Object Also known as: set_element
Set a value of the element for the given index.
59 60 61 |
# File 'lib/tty/vector.rb', line 59 def []=(indx, value) elements[indx] = value end |
#each ⇒ self
Iterate over each element in the vector
73 74 75 76 77 |
# File 'lib/tty/vector.rb', line 73 def each return to_enum unless block_given? to_ary.each { |element| yield element } self end |
#empty? ⇒ Boolean
Check if there are not elements.
93 94 95 |
# File 'lib/tty/vector.rb', line 93 def empty? to_ary.empty? end |
#size ⇒ Integer Also known as: length
Check number of elements.
102 103 104 |
# File 'lib/tty/vector.rb', line 102 def size to_ary.size end |
#to_a ⇒ Array
Return the vector elements in an array.
112 113 114 |
# File 'lib/tty/vector.rb', line 112 def to_a to_ary.dup end |
#to_ary ⇒ Object
Convert to array
@return [Array]
84 85 86 |
# File 'lib/tty/vector.rb', line 84 def to_ary @elements end |