Class: Unitwise::Measurement
Overview
A Measurement is a combination of a numeric value and a unit. You can think of this as a type of vector where the direction is the unit designation and the value is the magnitude. This is the primary class that outside code will interact with. Comes with conversion, comparison, and math methods.
Instance Method Summary collapse
-
#*(other) ⇒ Object
Multiply this measurement by a number or another measurement.
-
#**(other) ⇒ Object
Raise a measurement to a numeric power.
-
#+(other) ⇒ Object
Add another measurement to this unit.
-
#-(other) ⇒ Object
Subtract another measurement from this unit.
-
#/(other) ⇒ Object
Divide this measurement by a number or another measurement.
-
#coerce(other) ⇒ Object
Coerce a numeric to a a measurement for mathematical operations.
-
#convert_to(other_unit) ⇒ Object
Convert this measurement to a compatible unit.
-
#initialize(*args) ⇒ Measurement
constructor
Create a new Measurement Measurement::Unit.
-
#method_missing(meth, *args, &block) ⇒ Object
Will attempt to convert to a unit by method name.
-
#to_f ⇒ Object
Convert a measurement to a Float.
-
#to_i ⇒ Object
(also: #to_int)
Convert a measurement to an Integer.
-
#to_r ⇒ Object
Convert a measurement to a Rational.
Methods inherited from Scale
#atoms, #depth, #eql?, #expression, #hash, #inspect, #magnitude, #root_terms, #scalar, #simplified_value, #special?, #terms, #to_s, #unit=
Methods included from Compatible
#<=>, #compatible_with?, #composition, #composition_string, #dim, included
Constructor Details
#initialize(*args) ⇒ Measurement
Create a new Measurement Measurement::Unit
14 15 16 17 |
# File 'lib/unitwise/measurement.rb', line 14 def initialize(*args) super(*args) terms end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
Will attempt to convert to a unit by method name.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/unitwise/measurement.rb', line 132 def method_missing(meth, *args, &block) if args.empty? && !block_given? && (match = /\Ato_(\w+)\Z/.match(meth.to_s)) begin convert_to(match[1]) rescue ExpressionError super(meth, *args, &block) end else super(meth, *args, &block) end end |
Instance Method Details
#*(other) ⇒ Object
Multiply this measurement by a number or another measurement
41 42 43 44 |
# File 'lib/unitwise/measurement.rb', line 41 def *(other) operate(:*, other) || fail(TypeError, "Can't multiply #{self} by #{other}.") end |
#**(other) ⇒ Object
Raise a measurement to a numeric power.
80 81 82 83 84 85 86 |
# File 'lib/unitwise/measurement.rb', line 80 def **(other) if other.is_a?(Numeric) new(value ** other, unit ** other) else fail TypeError, "Can't raise #{self} to #{other} power." end end |
#+(other) ⇒ Object
Add another measurement to this unit. Units must be compatible.
61 62 63 |
# File 'lib/unitwise/measurement.rb', line 61 def +(other) combine(:+, other) || fail(TypeError, "Can't add #{other} to #{self}.") end |
#-(other) ⇒ Object
Subtract another measurement from this unit. Units must be compatible.
70 71 72 73 |
# File 'lib/unitwise/measurement.rb', line 70 def -(other) combine(:-, other) || fail(TypeError, "Can't subtract #{other} from #{self}.") end |
#/(other) ⇒ Object
Divide this measurement by a number or another measurement
52 53 54 |
# File 'lib/unitwise/measurement.rb', line 52 def /(other) operate(:/, other) || fail(TypeError, "Can't divide #{self} by #{other}") end |
#coerce(other) ⇒ Object
Coerce a numeric to a a measurement for mathematical operations
94 95 96 97 98 99 100 101 |
# File 'lib/unitwise/measurement.rb', line 94 def coerce(other) case other when Numeric return self.class.new(other, '1'), self else fail TypeError, "#{self.class} can't be coerced into #{other.class}" end end |
#convert_to(other_unit) ⇒ Object
Convert this measurement to a compatible unit. or a Measurement::Unit
26 27 28 29 30 31 32 33 |
# File 'lib/unitwise/measurement.rb', line 26 def convert_to(other_unit) other_unit = Unit.new(other_unit) if compatible_with?(other_unit) new(converted_value(other_unit), other_unit) else fail ConversionError, "Can't convert #{self} to #{other_unit}." end end |
#to_f ⇒ Object
Convert a measurement to a Float.
116 117 118 |
# File 'lib/unitwise/measurement.rb', line 116 def to_f Float(value) end |
#to_i ⇒ Object Also known as: to_int
Convert a measurement to an Integer.
107 108 109 |
# File 'lib/unitwise/measurement.rb', line 107 def to_i Integer(value) end |
#to_r ⇒ Object
Convert a measurement to a Rational.
124 125 126 |
# File 'lib/unitwise/measurement.rb', line 124 def to_r Rational(value) end |