class SIUnits::Unit
Constants
- UNITS_DEFINITION
Definition of SI Prefix Units, with name, aliases and scale
- UNIT_REGEX
Attributes
kind[R]
value[R]
Public Class Methods
new(*options)
click to toggle source
Create a new Unit
object. @return [Unit] @raise [ArgumentError] if absolute value of a temperature is less than absolute zero @raise [ArgumentError] if no unit is specified @raise [ArgumentError] if an invalid unit is specified
# 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
Public Instance Methods
<=>(comparison)
click to toggle source
==(comparison)
click to toggle source
# File lib/si_units/unit.rb, line 87 def ==(comparison) kind == comparison.kind end
best_scale()
click to toggle source
Public call to get a unit best representation @return String
# File lib/si_units/unit.rb, line 77 def best_scale @best_scale ||= best_value_with_scale end
convert_to(other)
click to toggle source
convert to a specified unit string or to the same unit as another Unit
# 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
Also aliased as: >>
to_s()
click to toggle source
# File lib/si_units/unit.rb, line 108 def to_s [best_scale, kind].join(' ') end
Private Instance Methods
best_value_with_scale()
click to toggle source
parse_unit()
click to toggle source
The parser
> Finds that the number range is contained¶ ↑
@return String
with the name of representation @raise ArgumentError
# File lib/si_units/unit.rb, line 124 def parse_unit case @value when 0 return "zero" when 1e-24..1e-21 return "yocto" when 1e-21..1e-18 return "atto" when 1e-18..1e-15 return "femto" when 1e-15..1e-12 return "pico" when 1e-12..1e-9 return "nano" when 1e-9..1e-6 return "micro" when 1e-6..1e-3 return "milli" when 1e-3..1e-2 return "centi" when 1e-2..1e-1 return "deci" when 1e-1..1e1 return "1" when 1e1..1e2 return "deca" when 1e2..1e3 return "hecto" when 1e3..1e6 return "kilo" when 1e6..1e9 return "mega" when 1e9..1e12 return "giga" when 1e12..1e15 return "tera" when 1e15..1e18 return "peta" when 1e18..1e21 return "exa" when 1e21..1e24 return "zetta" else raise ArgumentError, "Unit out of range" end end
scale()
click to toggle source
# File lib/si_units/unit.rb, line 182 def scale @scale ||= UNITS_DEFINITION[kind].last end
split_value(value)
click to toggle source
# File lib/si_units/unit.rb, line 186 def split_value(value) value.scan(UNIT_REGEX).flatten end
who_is_my_prefix?(prefix)
click to toggle source
# File lib/si_units/unit.rb, line 171 def who_is_my_prefix?(prefix) prefix unless UNITS_DEFINITION.has_key?(prefix) UNITS_DEFINITION.each do |key, value| return key if value[0].include?(prefix) end raise ArgumentError, "Unknown prefix" end