Class: SIUnits::Unit
- Inherits:
-
Object
- Object
- SIUnits::Unit
- 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] }
Instance Attribute Summary collapse
-
#unit_value ⇒ Object
readonly
Returns the value of attribute unit_value.
Instance Method Summary collapse
- #<=>(comparison) ⇒ Object
-
#best_scale ⇒ Object
Public call to get a unit best representation.
-
#convert_to(prefix) ⇒ Object
convert to a specified unit string or to the same units as another Unit.
-
#initialize(unit) ⇒ Object
constructor
Create a unit object Can be initialized with a number initialize a unit and parse, recognizing the scale of number.
Constructor Details
#initialize(unit) ⇒ Object
Create a unit object Can be initialized with a number initialize a unit and parse, recognizing the scale of number
52 53 54 55 |
# File 'lib/si_units/unit.rb', line 52 def initialize(unit) @unit_value = unit @unit_kind = parse_unit end |
Instance Attribute Details
#unit_value ⇒ Object (readonly)
Returns the value of attribute unit_value.
10 11 12 |
# File 'lib/si_units/unit.rb', line 10 def unit_value @unit_value end |
Instance Method Details
#<=>(comparison) ⇒ Object
64 65 66 |
# File 'lib/si_units/unit.rb', line 64 def <=>(comparison) UNITS_DEFINITION.find_index(@unit_kind) <=> UNITS_DEFINITION.find_index(comparison.unit_kind) end |
#best_scale ⇒ Object
Public call to get a unit best representation
59 60 61 |
# File 'lib/si_units/unit.rb', line 59 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
69 70 71 72 73 74 75 |
# File 'lib/si_units/unit.rb', line 69 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 |