Class: AUOM::Unit

Inherits:
Object
  • Object
show all
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

Constants included from Inspection

Inspection::INSPECT_FORMAT, Inspection::SCALAR_FORMAT, Inspection::UNIT_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equalization

#==

Methods included from Relational

#<=>

Methods included from Inspection

#inspect, prettify_unit_part

Methods included from Algebra

#add, #divide, #multiply, #subtract

Instance Attribute Details

#denominatorsRational (readonly)

Return denominators

Examples:


include AUOM
m = Unit.new(1, :meter)
m.denominators # => []

Returns:

  • (Rational)


52
53
54
# File 'lib/auom/unit.rb', line 52

def denominators
  @denominators
end

#numeratorsRational (readonly)

Return numerators

Examples:


include AUOM
m = Unit.new(1, :meter)
m.numerators # => [:meter]

Returns:

  • (Rational)


38
39
40
# File 'lib/auom/unit.rb', line 38

def numerators
  @numerators
end

#scalarRational (readonly)

Return scalar

Examples:


include AUOM
m = Unit.new(1, :meter)
m.scalar # => Rational(1, 1)

Returns:

  • (Rational)


24
25
26
# File 'lib/auom/unit.rb', line 24

def scalar
  @scalar
end

#unitArray (readonly)

Return unit descriptor

Examples:


u = Unit.new(1, [:meter, :meter], :euro)
u.unit # => [[:meter, :meter], [:euro]]

Returns:

  • (Array)


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

Parameters:

  • operand (Object)

Returns:

Raises:

  • (ArgumentError)

    raises argument error in case operand cannot be converted



206
207
208
209
210
211
212
213
# File 'lib/auom/unit.rb', line 206

def self.convert(operand)
  converted = try_convert(operand)
  unless converted
    fail ArgumentError, "Cannot convert #{operand.inspect} to #{self}"
  end

  converted
end

.new(scalar, numerators = nil, denominators = nil) ⇒ Unit

Instantiate a new unit

TODO: Move defaults coercions etc to .build method

Examples:


# A unitless unit
u = Unit.new(1)
u.unitless? # => true
u.scalar    # => Rational(1, 1)

# A unitless unit from string
u = Unit.new('1.5')
u.unitless? # => true
u.scalar    # => Rational(3, 2)

# A simple unit
u = Unit.new(1, :meter)
u.unitless?  # => false
u.numerators # => [:meter]
u.scalar     # => Rational(1, 1)

# A complex unit
u = Unit.new(Rational(1, 3), :euro, :meter)
u.fractions? # => true
u.scalar     # => Rational(1, 3)
u.inspect    # => <AUOM::Unit @scalar=~0.3333 euro/meter>
u.unit       # => [[:euro], [:meter]]

Parameters:

  • scalar (Rational)
  • numerators (Enumerable) (defaults to: nil)
  • denominators (Enumerable) (defaults to: nil)

Returns:



172
173
174
175
176
177
178
179
# 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, :/)

  super(scalar, *[numerators, denominators].map(&:sort)).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

Parameters:

  • operand (Object)

Returns:

  • (Unit)

    return unit in case operand can be converted

  • (nil)

    return nil in case operand can NOT be converted



227
228
229
230
231
232
233
234
# File 'lib/auom/unit.rb', line 227

def self.try_convert(operand)
  case operand
  when self
    operand
  when Integer, Rational
    new(operand)
  end
end

.unitsHash

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 built-in units symbols

Returns:

  • (Hash)


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

Parameters:

Returns:

  • (self)


189
190
191
192
193
# File 'lib/auom/unit.rb', line 189

def assert_same_unit(other)
  fail ArgumentError, 'Incompatible units' unless same_unit?(other)

  self
end

#same_unit?(other) ⇒ true, false

Test if units are the same

Examples:


a = Unit.new(1)
b = Unit.new(1, :euro)
c = Unit.new(2, :euro)

a.same_unit?(b) # => false
b.same_unit?(c) # => true

Parameters:

Returns:

  • (true)

    if units are the same

  • (false)

    otherwise



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

Examples:


Unit.new(1).unitless?         # => true
Unit.new(1, :meter).unitless ? # => false

Returns:

  • (true)

    return true if unit is unitless

  • (false)

    return false if unit is NOT unitless



106
107
108
# File 'lib/auom/unit.rb', line 106

def unitless?
  numerators.empty? && denominators.empty?
end