Class: Dcc::QuantityMath::Quantity
- Inherits:
-
Object
- Object
- Dcc::QuantityMath::Quantity
- Defined in:
- lib/dcc/quantity_math/quantity.rb
Overview
A simple (value, unit, uncertainty) value object used by
Dcc::QuantityMath::Real for arithmetic. Immutable.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#uncertainty ⇒ Object
readonly
Returns the value of attribute uncertainty.
-
#unit ⇒ Object
readonly
Returns the value of attribute unit.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(value:, unit: nil, uncertainty: nil) ⇒ Quantity
constructor
A new instance of Quantity.
-
#same_unit?(other) ⇒ Boolean
Whether units match (or both are nil).
- #to_s ⇒ Object
-
#uncertain? ⇒ Boolean
Whether the quantity carries uncertainty info.
Constructor Details
#initialize(value:, unit: nil, uncertainty: nil) ⇒ Quantity
Returns a new instance of Quantity.
15 16 17 18 19 |
# File 'lib/dcc/quantity_math/quantity.rb', line 15 def initialize(value:, unit: nil, uncertainty: nil) @value = cast_decimal(value) @unit = unit @uncertainty = uncertainty.nil? ? nil : cast_decimal(uncertainty) end |
Instance Attribute Details
#uncertainty ⇒ Object (readonly)
Returns the value of attribute uncertainty.
10 11 12 |
# File 'lib/dcc/quantity_math/quantity.rb', line 10 def uncertainty @uncertainty end |
#unit ⇒ Object (readonly)
Returns the value of attribute unit.
10 11 12 |
# File 'lib/dcc/quantity_math/quantity.rb', line 10 def unit @unit end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
10 11 12 |
# File 'lib/dcc/quantity_math/quantity.rb', line 10 def value @value end |
Instance Method Details
#same_unit?(other) ⇒ Boolean
Returns whether units match (or both are nil).
27 28 29 |
# File 'lib/dcc/quantity_math/quantity.rb', line 27 def same_unit?(other) unit == other.unit end |
#to_s ⇒ Object
31 32 33 34 35 |
# File 'lib/dcc/quantity_math/quantity.rb', line 31 def to_s u = uncertain? ? " ± #{uncertainty}" : "" n = unit ? " #{unit}" : "" "#{value}#{u}#{n}" end |
#uncertain? ⇒ Boolean
Returns whether the quantity carries uncertainty info.
22 23 24 |
# File 'lib/dcc/quantity_math/quantity.rb', line 22 def uncertain? !uncertainty.nil? end |