Class: SIUnits::Unit

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

Constant Summary collapse

UNITS_DEFINITION =

SI Prefix Units

{
  '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]
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(unit) ⇒ Unit

Returns a new instance of Unit.



40
41
42
43
# File 'lib/si_units/unit.rb', line 40

def initialize(unit)
  @unit_value = unit
  @unit_kind = parse_unit
end

Instance Attribute Details

#unit_valueObject (readonly)

Returns the value of attribute unit_value.



4
5
6
# File 'lib/si_units/unit.rb', line 4

def unit_value
  @unit_value
end

Instance Method Details

#<=>(comparison) ⇒ Object



49
50
51
# File 'lib/si_units/unit.rb', line 49

def <=>(comparison)
  UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind)
end

#best_scaleObject



45
46
47
# File 'lib/si_units/unit.rb', line 45

def best_scale
  @best_scale ||= best_value_with_scale
end

#convert_to(prefix) ⇒ Object

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



54
55
56
57
58
59
60
# File 'lib/si_units/unit.rb', line 54

def convert_to(prefix)
  return self if prefix.nil?
  scalar = prefix_is_defined?(prefix)
  absolute_unit_value = convert_base_prefix_to_value self.unit_value, scalar

  SIUnits::Unit.new(absolute_unit_value)
end