Class: Unitwise::Scale
- Inherits:
-
Object
- Object
- Unitwise::Scale
- Includes:
- Compatible
- Defined in:
- lib/unitwise/scale.rb
Overview
A Unitwise::Scale represents a value and a unit, sort of like a vector, it has two components. In this case, it’s a value and unit rather than a magnitude and direction. This class should be considered mostly privateish.
Direct Known Subclasses
Instance Method Summary collapse
-
#atoms ⇒ Array
List the atoms associated with this scale’s unit.
-
#depth ⇒ Integer
How far away is this instances unit from the deepest leve atom.
-
#eql?(other) ⇒ Boolean
Redefine hash equality to match the hashes.
-
#hash ⇒ Object
Redefine hash for apropriate hash/key lookup.
- #inspect ⇒ Object
-
#magnitude(scalar = scalar) ⇒ Numeric
Get a magnitude based on a linear scale value.
-
#root_terms ⇒ Array
The base terms this scale’s unit is derived from.
-
#scalar(magnitude = value) ⇒ Numeric
Get a scalar value for this scale.
-
#special? ⇒ true, false
Is this scale’s unit special?.
-
#terms ⇒ Array
List the terms associated with this scale’s unit.
-
#to_s ⇒ Object
Convert to a simple string representing the scale.
-
#unit=(value) ⇒ Object
Set the unit vector.
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included, #initialize
Instance Method Details
#atoms ⇒ Array
List the atoms associated with this scale’s unit.
19 20 21 |
# File 'lib/unitwise/scale.rb', line 19 def atoms unit.atoms end |
#depth ⇒ Integer
How far away is this instances unit from the deepest leve atom.
73 74 75 |
# File 'lib/unitwise/scale.rb', line 73 def depth unit.depth + 1 end |
#eql?(other) ⇒ Boolean
Redefine hash equality to match the hashes
97 98 99 |
# File 'lib/unitwise/scale.rb', line 97 def eql?(other) hash == other.hash end |
#hash ⇒ Object
Redefine hash for apropriate hash/key lookup
90 91 92 |
# File 'lib/unitwise/scale.rb', line 90 def hash [value, unit.to_s, self.class].hash end |
#inspect ⇒ Object
84 85 86 |
# File 'lib/unitwise/scale.rb', line 84 def inspect "#<#{self.class} value=#{value} unit=#{unit}>" end |
#magnitude(scalar = scalar) ⇒ Numeric
Get a magnitude based on a linear scale value. Only used by scales with special atoms in it’s hierarchy.
54 55 56 57 58 59 60 |
# File 'lib/unitwise/scale.rb', line 54 def magnitude(scalar = scalar) if special? unit.magnitude(scalar) else value * unit.magnitude end end |
#root_terms ⇒ Array
The base terms this scale’s unit is derived from
65 66 67 |
# File 'lib/unitwise/scale.rb', line 65 def root_terms unit.root_terms end |
#scalar(magnitude = value) ⇒ Numeric
Get a scalar value for this scale.
41 42 43 44 45 46 47 |
# File 'lib/unitwise/scale.rb', line 41 def scalar(magnitude = value) if special? unit.scalar(magnitude) else value * unit.scalar end end |
#special? ⇒ true, false
Is this scale’s unit special?
33 34 35 |
# File 'lib/unitwise/scale.rb', line 33 def special? unit.special? end |
#terms ⇒ Array
List the terms associated with this scale’s unit.
26 27 28 |
# File 'lib/unitwise/scale.rb', line 26 def terms unit.terms end |
#to_s ⇒ Object
Convert to a simple string representing the scale.
80 81 82 |
# File 'lib/unitwise/scale.rb', line 80 def to_s "#{value} #{unit}" end |
#unit=(value) ⇒ Object
Set the unit vector.
12 13 14 |
# File 'lib/unitwise/scale.rb', line 12 def unit=(value) @unit = value.is_a?(Unit) ? value : Unit.new(value) end |