Class: AUOM::Unit
- Inherits:
-
Object
- Object
- AUOM::Unit
- Includes:
- Algebra, Equalization, Inspection, Relational
- Defined in:
- lib/auom/unit.rb
Overview
A scalar with units
Constant Summary collapse
- UNITS =
These constants can easily be changed by an application specific subclass that overrides AUOM::Unit.units with an own hash!
{ item: [1, :item], liter: [1, :liter], pack: [1, :pack], can: [1, :can], kilogramm: [1, :kilogramm], euro: [1, :euro], meter: [1, :meter], kilometer: [1000, :meter] }.freeze
Instance Attribute Summary collapse
-
#denominators ⇒ Rational
readonly
Return denominators.
-
#numerators ⇒ Rational
readonly
Return numerators.
-
#scalar ⇒ Rational
readonly
Return scalar.
-
#unit ⇒ Array
readonly
Return unit descriptor.
Class Method Summary collapse
-
.convert(operand) ⇒ Unit
private
Return converted operand or raise error.
-
.new(scalar, numerators = nil, denominators = nil) ⇒ Unit
Instancitate a new unit.
-
.try_convert(operand) ⇒ Unit?
private
Return converted operand or nil.
-
.units ⇒ Hash
private
Return buildin units symbols.
Instance Method Summary collapse
-
#assert_same_unit(other) ⇒ self
private
Assert units are the same.
-
#same_unit?(other) ⇒ true, false
Test if units are the same.
-
#unitless? ⇒ true, false
Check for unitless unit.
Methods included from Relational
#greater_than?, #greater_than_or_equal_to?, #less_than?, #less_than_or_equal_to?
Methods included from Inspection
Methods included from Equalization
Methods included from Algebra
#add, #divide, #multiply, #substract
Instance Attribute Details
#denominators ⇒ Rational (readonly)
Return denominators
52 53 54 |
# File 'lib/auom/unit.rb', line 52 def denominators @denominators end |
#numerators ⇒ Rational (readonly)
Return numerators
38 39 40 |
# File 'lib/auom/unit.rb', line 38 def numerators @numerators end |
#scalar ⇒ Rational (readonly)
Return scalar
24 25 26 |
# File 'lib/auom/unit.rb', line 24 def scalar @scalar end |
#unit ⇒ Array (readonly)
Return unit descriptor
65 66 67 |
# File 'lib/auom/unit.rb', line 65 def unit @unit end |
Class Method Details
.convert(operand) ⇒ Unit
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return converted operand or raise error
209 210 211 212 213 214 215 |
# File 'lib/auom/unit.rb', line 209 def self.convert(operand) converted = try_convert(operand) unless converted raise ArgumentError, "Cannot convert #{operand.inspect} to #{self}" end converted end |
.new(scalar, numerators = nil, denominators = nil) ⇒ Unit
Instancitate a new unit
TODO: Move defaults coercions etc to .build method
172 173 174 175 176 177 178 179 180 |
# File 'lib/auom/unit.rb', line 172 def self.new(scalar, numerators = nil, denominators = nil) scalar = rational(scalar) scalar, numerators = resolve([*numerators], scalar, :*) scalar, denominators = resolve([*denominators], scalar, :/) # sorting on #to_s as Symbol#<=> is not present on 1.8.7 super(scalar, *[numerators, denominators].map { |base| base.sort_by(&:to_s) }).freeze end |
.try_convert(operand) ⇒ Unit?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return converted operand or nil
229 230 231 232 233 234 235 236 |
# File 'lib/auom/unit.rb', line 229 def self.try_convert(operand) case operand when self operand when Fixnum, Rational new(operand) end end |
.units ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Return buildin units symbols
87 88 89 |
# File 'lib/auom/unit.rb', line 87 def self.units UNITS end |
Instance Method Details
#assert_same_unit(other) ⇒ self
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assert units are the same
190 191 192 193 194 195 196 |
# File 'lib/auom/unit.rb', line 190 def assert_same_unit(other) unless same_unit?(other) raise ArgumentError, 'Incompatible units' end self end |
#same_unit?(other) ⇒ true, false
Test if units are the same
131 132 133 |
# File 'lib/auom/unit.rb', line 131 def same_unit?(other) other.unit.eql?(unit) end |
#unitless? ⇒ true, false
Check for unitless unit
106 107 108 |
# File 'lib/auom/unit.rb', line 106 def unitless? numerators.empty? and denominators.empty? end |