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 magnitued. 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.
-
#**(number) ⇒ 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, #functional, #inspect, #root_terms, #scalar, #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
15 16 17 18 19 20 |
# File 'lib/unitwise/measurement.rb', line 15 def initialize(*args) super(*args) if terms.nil? raise ExpressionError, "Could not evaluate `#{unit}`." end 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.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/unitwise/measurement.rb', line 133 def method_missing(meth, *args, &block) if args.empty? && !block_given? && (match = /\Ato_(\w+)\Z/.match(meth)) 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
44 45 46 |
# File 'lib/unitwise/measurement.rb', line 44 def *(other) operate(:*, other) || raise(TypeError, "Can't multiply #{self} by #{other}.") end |
#**(number) ⇒ Object
Raise a measurement to a numeric power.
81 82 83 84 85 86 87 |
# File 'lib/unitwise/measurement.rb', line 81 def **(number) if number.is_a?(Numeric) new( value ** number, unit ** number ) else raise TypeError, "Can't raise #{self} to #{number} power." end end |
#+(other) ⇒ Object
Add another measurement to this unit. Units must be compatible.
63 64 65 |
# File 'lib/unitwise/measurement.rb', line 63 def +(other) combine(:+, other) || raise(TypeError, "Can't add #{other} to #{self}.") end |
#-(other) ⇒ Object
Subtract another measurement from this unit. Units must be compatible.
72 73 74 |
# File 'lib/unitwise/measurement.rb', line 72 def -(other) combine(:-, other) || raise(TypeError, "Can't subtract #{other} from #{self}.") end |
#/(other) ⇒ Object
Divide this measurement by a number or another measurement
54 55 56 |
# File 'lib/unitwise/measurement.rb', line 54 def /(other) operate(:/, other) || raise(TypeError, "Can't divide #{self} by #{other}") end |
#coerce(other) ⇒ Object
Coerce a numeric to a a measurement for mathematical operations
95 96 97 98 99 100 101 102 |
# File 'lib/unitwise/measurement.rb', line 95 def coerce(other) case other when Numeric return self.class.new(other, '1'), self else raise 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
29 30 31 32 33 34 35 36 |
# File 'lib/unitwise/measurement.rb', line 29 def convert_to(other_unit) other_unit = Unit.new(other_unit) if compatible_with?(other_unit) new(converted_value(other_unit), other_unit) else raise ConversionError, "Can't convert #{self} to #{other_unit}." end end |
#to_f ⇒ Object
Convert a measurement to a Float.
117 118 119 |
# File 'lib/unitwise/measurement.rb', line 117 def to_f Float(value) end |
#to_i ⇒ Object Also known as: to_int
Convert a measurement to an Integer.
108 109 110 |
# File 'lib/unitwise/measurement.rb', line 108 def to_i Integer(value) end |
#to_r ⇒ Object
Convert a measurement to a Rational.
125 126 127 |
# File 'lib/unitwise/measurement.rb', line 125 def to_r Rational(value) end |