Module: VectorNumber::Enumerating
Overview
Methods for enumerating values of the number.
Enumerable
is included so its methods can be used.
Instance Method Summary collapse
-
#[](unit) ⇒ Integer, ...
Get the coefficient for the unit.
-
#coefficients ⇒ Array<Integer, Float, Rational, BigDecimal>
(also: #values)
Get a list of non-zero coefficients.
-
#each(&block) ⇒ Object
(also: #each_pair)
Iterate through every pair of unit and coefficient.
-
#to_h(&block) ⇒ Hash{Object => Integer, Float, Rational, BigDecimal}
Get mutable hash with vector’s data.
-
#unit?(unit) ⇒ Boolean
(also: #key?)
Check if a unit has a non-zero coefficient.
-
#units ⇒ Array<Object>
(also: #keys)
Get a list of units with non-zero coefficients.
Instance Method Details
#[](unit) ⇒ Integer, ...
Get the coefficient for the unit.
If the unit?(unit) is false, 0 is returned. Note that units for real and imaginary parts are VectorNumber::R and VectorNumber::I respectively.
111 |
# File 'lib/vector_number/enumerating.rb', line 111 def [](unit) = @data[unit] |
#coefficients ⇒ Array<Integer, Float, Rational, BigDecimal> Also known as: values
Get a list of non-zero coefficients.
72 |
# File 'lib/vector_number/enumerating.rb', line 72 def coefficients = @data.values |
#each {|unit, coefficient| ... } ⇒ VectorNumber #each ⇒ Enumerator Also known as: each_pair
Iterate through every pair of unit and coefficient. Returns Enumerator
(with set size
) if no block is given.
39 40 41 42 43 44 |
# File 'lib/vector_number/enumerating.rb', line 39 def each(&block) return to_enum { size } unless block_given? @data.each(&block) self end |
#to_h(&block) ⇒ Hash{Object => Integer, Float, Rational, BigDecimal}
Get mutable hash with vector’s data.
Returned hash has a default value of 0.
88 89 90 91 92 93 94 95 |
# File 'lib/vector_number/enumerating.rb', line 88 def to_h(&block) # TODO: Remove block argument. if block_given? @data.to_h(&block) else @data.dup end end |
#unit?(unit) ⇒ Boolean Also known as: key?
Check if a unit has a non-zero coefficient.
123 |
# File 'lib/vector_number/enumerating.rb', line 123 def unit?(unit) = @data.key?(unit) |
#units ⇒ Array<Object> Also known as: keys
Get a list of units with non-zero coefficients.
58 |
# File 'lib/vector_number/enumerating.rb', line 58 def units = @data.keys |