Class: SIUnits::Unit

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/si_units/unit.rb

Constant Summary collapse

UNITS_DEFINITION =

Definition of SI Prefix Units, with name, aliases and scale

{
  'googol' => [%w{googol},            1e100],
  'yebi'   => [%w{Yi Yebi yebi},      2**80],
  'zebi'   => [%w{Zi Zebi zebi},      2**70],
  'exi'    => [%w{Ei Exi exi},        2**60],
  'pebi'   => [%w{Pi Pebi pebi},      2**50],
  'tebi'   => [%w{Ti Tebi tebi},      2**40],
  'gibi'   => [%w{Gi Gibi gibi},      2**30],
  'mebi'   => [%w{Mi Mebi mebi},      2**20],
  'kibi'   => [%w{Ki Kibi kibi},      2**10],
  'yotta'  => [%w{Y Yotta yotta},     1e24],
  'zetta'  => [%w{Z Zetta zetta},     1e21],
  'exa'    => [%w{E Exa exa},         1e18],
  'peta'   => [%w{P Peta peta},       1e15],
  'tera'   => [%w{T Tera tera},       1e12],
  'giga'   => [%w{G Giga giga},       1e9],
  'mega'   => [%w{M Mega mega},       1e6],
  'kilo'   => [%w{k kilo},            1e3],
  'hecto'  => [%w{h Hecto hecto},     1e2],
  'deca'   => [%w{da Deca deca deka}, 1e1],
  '1'      => [%w{const},                1],
  'deci'   => [%w{d Deci deci},       1e-1],
  'centi'  => [%w{c Centi centi},     1e-2],
  'milli'  => [%w{m Milli milli},     1e-3],
  'micro'  => [%w{u Micro micro},     1e-6],
  'nano'   => [%w{n Nano nano},       1e-9],
  'pico'   => [%w{p Pico pico},       1e-12],
  'femto'  => [%w{f Femto femto},     1e-15],
  'atto'   => [%w{a Atto atto},       1e-18],
  'zepto'  => [%w{z Zepto zepto},     1e-21],
  'yocto'  => [%w{y Yocto yocto},     1e-24],
  'zero'   => [%w{zero},                0.0]
}
UNIT_REGEX =
/\d+[\.?]\d+|\w+/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*options) ⇒ Unit

Create a new Unit object.

Raises:

  • (ArgumentError)

    if absolute value of a temperature is less than absolute zero

  • (ArgumentError)

    if no unit is specified

  • (ArgumentError)

    if an invalid unit is specified



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/si_units/unit.rb', line 54

def initialize(*options)

  raise ArgumentError, "Unit can't be initialized without args" if options[0].nil?

  case options[0]
  when Numeric
    # Conversion use a scale
    @value = options.first
    @kind = parse_unit

  when String
    value, prefix = *split_value(options[0])
    @kind = who_is_my_prefix?(prefix)
    # Value is absolute, needs convert to scale of prefix
    @value = value.to_f * scale

  else
    raise ArgumentError, "Invalid Unit Format"
  end
end

Instance Attribute Details

#kindObject (readonly)

Returns the value of attribute kind.



11
12
13
# File 'lib/si_units/unit.rb', line 11

def kind
  @kind
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/si_units/unit.rb', line 11

def value
  @value
end

Instance Method Details

#<=>(comparison) ⇒ Object

Comparable units

> Compare with scale of kinds



83
84
85
# File 'lib/si_units/unit.rb', line 83

def <=>(comparison)
  UNITS_DEFINITION[kind].last <=> UNITS_DEFINITION[comparison.kind].last
end

#==(comparison) ⇒ Object



87
88
89
# File 'lib/si_units/unit.rb', line 87

def ==(comparison)
  kind == comparison.kind
end

#best_scaleObject

Public call to get a unit best representation

Returns:

  • String



77
78
79
# File 'lib/si_units/unit.rb', line 77

def best_scale
   @best_scale ||= best_value_with_scale
end

#convert_to(other) ⇒ Object Also known as: >>

convert to a specified unit string or to the same unit as another Unit



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/si_units/unit.rb', line 92

def convert_to(other)
  return self if other.nil?

  case other
    when Unit
      return self if other == self
      target = other
    when String
      target = SIUnits::Unit.new(other.to_f)
    else
      raise ArgumentError, "Unknown target units"
  end
end

#to_sObject



108
109
110
# File 'lib/si_units/unit.rb', line 108

def to_s
  [best_scale, kind].join(' ')
end