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.
- #expression ⇒ Object
-
#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.
- #simplified_value ⇒ Object
-
#special? ⇒ true, false
Is this scale’s unit special?.
-
#terms ⇒ Array
List the terms associated with this scale’s unit.
-
#to_s(mode = nil) ⇒ Object
Convert to a simple string representing the scale.
-
#unit=(value) ⇒ Object
Set the unit vector.
- #value=(value) ⇒ Object
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.
77 78 79 |
# File 'lib/unitwise/scale.rb', line 77 def depth unit.depth + 1 end |
#eql?(other) ⇒ Boolean
Redefine hash equality to match the hashes
120 121 122 |
# File 'lib/unitwise/scale.rb', line 120 def eql?(other) hash == other.hash end |
#expression ⇒ Object
97 98 99 |
# File 'lib/unitwise/scale.rb', line 97 def expression unit.expression end |
#hash ⇒ Object
Redefine hash for apropriate hash/key lookup
113 114 115 |
# File 'lib/unitwise/scale.rb', line 113 def hash [value, unit.to_s, self.class].hash end |
#inspect ⇒ Object
107 108 109 |
# File 'lib/unitwise/scale.rb', line 107 def inspect "#<#{self.class} value=#{simplified_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.
58 59 60 61 62 63 64 |
# File 'lib/unitwise/scale.rb', line 58 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
69 70 71 |
# File 'lib/unitwise/scale.rb', line 69 def root_terms unit.root_terms end |
#scalar(magnitude = value) ⇒ Numeric
Get a scalar value for this scale.
45 46 47 48 49 50 51 |
# File 'lib/unitwise/scale.rb', line 45 def scalar(magnitude = value) if special? unit.scalar(magnitude) else value * unit.scalar end end |
#simplified_value ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/unitwise/scale.rb', line 82 def simplified_value if value.is_a?(Integer) value elsif (i = Integer(value)) == value i elsif value.is_a?(Float) value elsif (f = Float(value)) == value f else value end end |
#special? ⇒ true, false
Is this scale’s unit special?
37 38 39 |
# File 'lib/unitwise/scale.rb', line 37 def special? unit.special? end |
#terms ⇒ Array
List the terms associated with this scale’s unit.
30 31 32 |
# File 'lib/unitwise/scale.rb', line 30 def terms unit.terms end |
#to_s(mode = nil) ⇒ Object
Convert to a simple string representing the scale.
103 104 105 |
# File 'lib/unitwise/scale.rb', line 103 def to_s(mode = nil) "#{simplified_value} #{unit.to_s(mode)}" 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 |
#value=(value) ⇒ Object
23 24 25 |
# File 'lib/unitwise/scale.rb', line 23 def value=(value) @value = BigDecimal(value.to_s) end |