class Unitwise::Term
A Term
is the combination of an atom, prefix, factor and annotation. Not all properties have to be present. Examples: 'g', 'mm', 'mi2', '4', 'kJ{Electric Potential}'
Public Instance Methods
Term
exponentiation. Raise a term to a numeric power. params other [Numeric] @return [Term]
# 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
Determine how far away a unit is from a base unit. @return [Integer] @api public
# File lib/unitwise/term.rb, line 32 def depth atom ? atom.depth + 1 : 0 end
The exponent for this term. The default value is 1. @return [Numeric] @api public
# File lib/unitwise/term.rb, line 54 def exponent super || 1 end
The multiplication factor for this term. The default value is 1. @return [Numeric] @api public
# File lib/unitwise/term.rb, line 47 def factor super || 1 end
Calculate the magnitude for this term @param scalar [Numeric] The scalar for which you want the magnitude @return [Numeric] The magnitude on this scale. @api public
# File lib/unitwise/term.rb, line 70 def magnitude(scalar = scalar()) calculate(atom ? atom.magnitude(scalar) : 1) end
The base units this term is derived from @return [Array] An array of Unitwise::Term
@api public
# 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
The unitless scalar value for this term. @param magnitude [Numeric] The magnitude to calculate the scalar for. @return [Numeric] The unitless linear scalar value. @api public
# File lib/unitwise/term.rb, line 62 def scalar(magnitude = 1) calculate(atom ? atom.scalar(magnitude) : magnitude) end
Is this term special? @return [true, false]
# File lib/unitwise/term.rb, line 25 def special? atom.special? rescue false end
Determine if this is the last term in the scale chain @return [true, false] @api public
# File lib/unitwise/term.rb, line 40 def terminal? depth <= 3 end
# 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
Private Instance Methods
@api private
# File lib/unitwise/term.rb, line 128 def calculate(value) (factor * (prefix ? prefix.scalar : 1) * value) ** exponent end
Multiply or divide a term @api private
# File lib/unitwise/term.rb, line 134 def operate(operator, other) exp = operator == '/' ? -1 : 1 if other.respond_to?(:terms) Unit.new(other.terms.map { |t| t ** exp } << self) elsif other.respond_to?(:atom) Unit.new([self, other ** exp]) elsif other.is_a?(Numeric) self.class.new(to_hash.merge(:factor => factor.send(operator, other))) end end