module Reality::Util::Parse

Constants

SCALES

See “Short scale”: en.wikipedia.org/wiki/Long_and_short_scales#Comparison

SCALES_REGEXP

Public Instance Methods

fetch_scale(str) click to toggle source
# File lib/reality/util/parsers.rb, line 46
def fetch_scale(str)
  _, res = SCALES.detect{|key, val| str.start_with?(key)}

  res or fail("Scale not found: #{str} for #{self}")
end
number(str) click to toggle source
# File lib/reality/util/parsers.rb, line 18
def number(str)
  str = str.gsub(',', '').tr('−', '-')
  case str
  when /^-?\d+$/
    str.to_i
  when /^-?\d+\.\d+$/
    str.to_f
  else
    nil
  end
end
scaled_number(str) click to toggle source
# File lib/reality/util/parsers.rb, line 7
def scaled_number(str)
  match, amount, scale = */^([0-9.,]+)[[:space:]]*(#{SCALES_REGEXP})?/.match(str)
  match or return nil

  if scale
    number(amount) * fetch_scale(scale)
  else
    number(amount)
  end
end