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.
-
#magnitude(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(mode = :primary_code) ⇒ Object
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included, #initialize
Instance Method Details
#*(other) ⇒ Term
Term multiplication. Multiply by a Unit, another Term, or a Numeric. params other [Unit, Term, Numeric]
91 92 93 94 |
# File 'lib/unitwise/term.rb', line 91 def *(other) operate('*', other) || fail(TypeError, "Can't multiply #{ self } by #{ other }.") end |
#**(other) ⇒ Term
Term exponentiation. Raise a term to a numeric power. params other [Numeric]
108 109 110 111 112 113 114 |
# File 'lib/unitwise/term.rb', line 108 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]
99 100 101 102 |
# File 'lib/unitwise/term.rb', line 99 def /(other) operate('/', other) || fail(TypeError, "Can't divide #{ self } by #{ other }.") end |
#atom=(value) ⇒ Object
Set the atom. Atom
12 13 14 |
# File 'lib/unitwise/term.rb', line 12 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.
32 33 34 |
# File 'lib/unitwise/term.rb', line 32 def depth atom ? atom.depth + 1 : 0 end |
#exponent ⇒ Numeric
The exponent for this term. The default value is 1.
54 55 56 |
# File 'lib/unitwise/term.rb', line 54 def exponent super || 1 end |
#factor ⇒ Numeric
The multiplication factor for this term. The default value is 1.
47 48 49 |
# File 'lib/unitwise/term.rb', line 47 def factor super || 1 end |
#magnitude(scalar = scalar()) ⇒ Numeric
Calculate the magnitude for this term
70 71 72 |
# File 'lib/unitwise/term.rb', line 70 def magnitude(scalar = scalar()) calculate(atom ? atom.magnitude(scalar) : 1) end |
#prefix=(value) ⇒ Object
Set the prefix. a Prefix
19 20 21 |
# File 'lib/unitwise/term.rb', line 19 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
77 78 79 80 81 82 83 84 85 |
# File 'lib/unitwise/term.rb', line 77 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.
62 63 64 |
# File 'lib/unitwise/term.rb', line 62 def scalar(magnitude = 1) calculate(atom ? atom.scalar(magnitude) : magnitude) end |
#special? ⇒ true, false
Is this term special?
25 26 27 |
# File 'lib/unitwise/term.rb', line 25 def special? atom.special? rescue false end |
#terminal? ⇒ true, false
Determine if this is the last term in the scale chain
40 41 42 |
# File 'lib/unitwise/term.rb', line 40 def terminal? depth <= 3 end |
#to_s(mode = :primary_code) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/unitwise/term.rb', line 116 def to_s(mode = :primary_code) [ (factor if factor != 1), (prefix.send(mode) if prefix), (atom.send(mode) if atom), (exponent if exponent != 1) ].compact.join('') end |