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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Relational

#greater_than?, #greater_than_or_equal_to?, #less_than?, #less_than_or_equal_to?

Methods included from Inspection

#inspect

Methods included from Equalization

#==

Methods included from Algebra

#add, #divide, #multiply, #substract

Instance Attribute Details

#denominatorsRational (readonly)

Return denominators

Examples:


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

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



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

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.sclar      # => 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
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

Parameters:

  • operand (Object)

Returns:

  • (Unit)

    return unit in case operand can be converted

  • (nil)

    return nil in case operand can NOT be converted



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

.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 buildin 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)


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

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)

    otehrwise



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? and denominators.empty?
end