Class: Unitwise::Term
- Inherits:
-
Object
- Object
- Unitwise::Term
- Includes:
- Compatible
- Defined in:
- lib/unitwise/term.rb
Overview
A Term is the combination of an atom, prefix, factor and annotation. Not all properties have to be present. Examples: 'g', 'mm', 'mi2', '4', 'kJPotential'
Instance Method Summary collapse
-
#*(other) ⇒ Term
Term multiplication.
-
#**(other) ⇒ Term
Term exponentiation.
-
#/(other) ⇒ Term
Term division.
-
#atom=(value) ⇒ Object
Set the atom.
-
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
-
#exponent ⇒ Numeric
The exponent for this term.
-
#factor ⇒ Numeric
The multiplication factor for this term.
-
#initialize(*args) ⇒ Term
constructor
Setup a new term.
-
#inverse_scalar(scalar = scalar) ⇒ Numeric
Calculate the magnitude for this term.
-
#prefix=(value) ⇒ Object
Set the prefix.
-
#root_terms ⇒ Array
The base units this term is derived from.
-
#scalar(magnitude = 1) ⇒ Numeric
The unitless scalar value for this term.
-
#special? ⇒ true, false
Is this term special?.
-
#terminal? ⇒ true, false
Determine if this is the last term in the scale chain.
-
#to_s ⇒ String
String representation for this term.
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included
Constructor Details
#initialize(*args) ⇒ Term
Setup a new term. Send a hash of properties, or ordered property values.
11 12 13 14 |
# File 'lib/unitwise/term.rb', line 11 def initialize(*args) super(*args) freeze end |
Instance Method Details
#*(other) ⇒ Term
Term multiplication. Multiply by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric]
97 98 99 100 101 102 103 104 105 |
# File 'lib/unitwise/term.rb', line 97 def *(other) if other.respond_to?(:terms) Unit.new(other.terms << self) elsif other.respond_to?(:atom) Unit.new([self, other]) elsif other.is_a?(Numeric) self.class.new(to_hash.merge(factor: factor * other)) end end |
#**(other) ⇒ Term
Term exponentiation. Raise a term to a numeric power. params other [Numeric]
123 124 125 126 127 128 129 |
# File 'lib/unitwise/term.rb', line 123 def **(other) if other.is_a?(Numeric) self.class.new(to_hash.merge(exponent: exponent * other)) else fail TypeError, "Can't raise #{self} to #{other}." end end |
#/(other) ⇒ Term
Term division. Divide by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric]
110 111 112 113 114 115 116 117 118 |
# File 'lib/unitwise/term.rb', line 110 def /(other) if other.respond_to?(:terms) Unit.new(other.terms.map { |t| t ** -1 } << self) elsif other.respond_to?(:atom) Unit.new([self, other ** -1]) elsif other.is_a?(Numeric) self.class.new(to_hash.merge(factor: factor / other)) end end |
#atom=(value) ⇒ Object
Set the atom. Atom
20 21 22 |
# File 'lib/unitwise/term.rb', line 20 def atom=(value) value.is_a?(Atom) ? super(value) : super(Atom.find(value.to_s)) end |
#depth ⇒ Integer
Determine how far away a unit is from a base unit.
40 41 42 |
# File 'lib/unitwise/term.rb', line 40 def depth atom ? atom.depth + 1 : 0 end |
#exponent ⇒ Numeric
The exponent for this term. The default value is 1.
61 62 63 |
# File 'lib/unitwise/term.rb', line 61 def exponent super || 1 end |
#factor ⇒ Numeric
The multiplication factor for this term. The default value is 1.
54 55 56 |
# File 'lib/unitwise/term.rb', line 54 def factor super || 1 end |
#inverse_scalar(scalar = scalar) ⇒ Numeric
Calculate the magnitude for this term
77 78 79 |
# File 'lib/unitwise/term.rb', line 77 def inverse_scalar(scalar = scalar) calculate(atom ? atom.inverse_scalar(scalar) : 1) end |
#prefix=(value) ⇒ Object
Set the prefix. a Prefix
27 28 29 |
# File 'lib/unitwise/term.rb', line 27 def prefix=(value) value.is_a?(Prefix) ? super(value) : super(Prefix.find(value.to_s)) end |
#root_terms ⇒ Array
The base units this term is derived from
84 85 86 87 88 89 90 91 92 |
# File 'lib/unitwise/term.rb', line 84 def root_terms if terminal? [self] else atom.scale.root_terms.map do |t| self.class.new(atom: t.atom, exponent: t.exponent * exponent) end end end |
#scalar(magnitude = 1) ⇒ Numeric
The unitless scalar value for this term.
69 70 71 |
# File 'lib/unitwise/term.rb', line 69 def scalar(magnitude = 1) calculate(atom ? atom.scalar(magnitude) : 1) end |
#special? ⇒ true, false
Is this term special?
33 34 35 |
# File 'lib/unitwise/term.rb', line 33 def special? atom.special? rescue false end |
#terminal? ⇒ true, false
Determine if this is the last term in the scale chain
47 48 49 |
# File 'lib/unitwise/term.rb', line 47 def terminal? depth <= 3 end |
#to_s ⇒ String
String representation for this term.
133 134 135 136 |
# File 'lib/unitwise/term.rb', line 133 def to_s [(factor if factor != 1), prefix.to_s, atom.to_s, (exponent if exponent != 1)].compact.join('') end |