Class: Alchemist::Measurement
- Inherits:
-
Object
- Object
- Alchemist::Measurement
- Includes:
- Comparable
- Defined in:
- lib/alchemist/geospatial.rb,
lib/alchemist/measurement.rb
Instance Attribute Summary collapse
-
#exponent ⇒ Object
readonly
Returns the value of attribute exponent.
-
#unit_name ⇒ Object
readonly
Returns the value of attribute unit_name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #*(multiplicand) ⇒ Object
- #+(measurement) ⇒ Object
- #-(measurement) ⇒ Object
- #/(measurement) ⇒ Object
- #<=>(other) ⇒ Object
- #base(unit_type) ⇒ Object
- #coerce(number) ⇒ Object
- #geospatial ⇒ Object
-
#initialize(value, unit_name, exponent = 1.0) ⇒ Measurement
constructor
A new instance of Measurement.
- #per ⇒ Object
- #shared_types(other_unit_name) ⇒ Object
- #to(type = nil) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize(value, unit_name, exponent = 1.0) ⇒ Measurement
Returns a new instance of Measurement.
9 10 11 12 13 |
# File 'lib/alchemist/measurement.rb', line 9 def initialize value, unit_name, exponent = 1.0 @value = value.to_f @unit_name = unit_name @exponent = exponent end |
Instance Attribute Details
#exponent ⇒ Object (readonly)
Returns the value of attribute exponent.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def exponent @exponent end |
#unit_name ⇒ Object (readonly)
Returns the value of attribute unit_name.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def unit_name @unit_name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def value @value end |
Instance Method Details
#*(multiplicand) ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/alchemist/measurement.rb', line 45 def * multiplicand if multiplicand.is_a?(Numeric) Measurement.new(value * multiplicand, unit_name, exponent) else try_raising_dimension(multiplicand) end end |
#+(measurement) ⇒ Object
27 28 29 30 31 |
# File 'lib/alchemist/measurement.rb', line 27 def + measurement ensure_shared_type!(measurement) converted = measurement.to(unit_name) Measurement.new(value + converted.value, unit_name, exponent) end |
#-(measurement) ⇒ Object
33 34 35 36 37 |
# File 'lib/alchemist/measurement.rb', line 33 def - measurement ensure_shared_type!(measurement) converted = measurement.to(unit_name) Measurement.new(value - converted.value, unit_name, exponent) end |
#/(measurement) ⇒ Object
39 40 41 42 43 |
# File 'lib/alchemist/measurement.rb', line 39 def / measurement ensure_shared_type!(measurement) dividend = measurement.is_a?(Measurement) ? measurement.to(unit_name).to_f / exponent : measurement Measurement.new(value / dividend, unit_name, exponent).value end |
#<=>(other) ⇒ Object
70 71 72 |
# File 'lib/alchemist/measurement.rb', line 70 def <=>(other) (self.to_f * exponent).to_f <=> other.to(unit_name).to_f end |
#base(unit_type) ⇒ Object
53 54 55 56 |
# File 'lib/alchemist/measurement.rb', line 53 def base unit_type conversion_base = conversion_base_for(unit_type) convert_to_base conversion_base end |
#coerce(number) ⇒ Object
82 83 84 |
# File 'lib/alchemist/measurement.rb', line 82 def coerce(number) [self, number] end |
#geospatial ⇒ Object
5 6 7 |
# File 'lib/alchemist/geospatial.rb', line 5 def geospatial Alchemist::Earth.new(self).geospatial end |
#per ⇒ Object
15 16 17 |
# File 'lib/alchemist/measurement.rb', line 15 def per CompoundMeasurement.new self end |
#shared_types(other_unit_name) ⇒ Object
78 79 80 |
# File 'lib/alchemist/measurement.rb', line 78 def shared_types other_unit_name types & Alchemist.measurement_for(other_unit_name) end |
#to(type = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/alchemist/measurement.rb', line 19 def to type = nil if type convertor.send(type) else convertor end end |
#to_f ⇒ Object
66 67 68 |
# File 'lib/alchemist/measurement.rb', line 66 def to_f @value end |
#to_i ⇒ Object
62 63 64 |
# File 'lib/alchemist/measurement.rb', line 62 def to_i value.to_i end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/alchemist/measurement.rb', line 58 def to_s value.to_s end |
#types ⇒ Object
74 75 76 |
# File 'lib/alchemist/measurement.rb', line 74 def types Alchemist.measurement_for(unit_name) end |