class TrickBag::Numeric::KmgtNumericString::ToNumberConverter

This class is here to simplify implementation and is not intended to be instantiated by users of this module.

Public Class Methods

new(string) click to toggle source
# File lib/trick_bag/numeric/kmgt_numeric_string.rb, line 77
def initialize(string)
  @string = string.dup.gsub('_', '')
  assert_valid_input(@string)
end

Public Instance Methods

assert_valid_input(string) click to toggle source
# File lib/trick_bag/numeric/kmgt_numeric_string.rb, line 82
def assert_valid_input(string)
  unless VALID_STRING_REGEX.match(string)
    raise ArgumentError.new(
        "Bad arg (#{string}); must be an integer, optionally followed by one of [" +
        MULTIPLIERS.keys.join + '], optionally with underscores.')
  end
end
to_number() click to toggle source
# File lib/trick_bag/numeric/kmgt_numeric_string.rb, line 90
def to_number
  last_char = @string[-1]
  if VALID_STRING_SUFFIXES.include?(last_char)
    multiplier = MULTIPLIERS[last_char]
    Integer(@string.chop) * multiplier
  else
    Integer(@string)
  end
end