Class: Object

Inherits:
BasicObject
Defined in:
parser/lib/uliska_parser/extensions.rb

Instance Method Summary (collapse)

Instance Method Details

- (Object) to_num

Convert to one of integer, float etc depending on format. If it does not look like number, return self unmodified.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'parser/lib/uliska_parser/extensions.rb', line 43

def to_num

  number = /^([-+]?\d+(\.\d+)?)(\s?[GMK])?$/

  return self unless self.is_a? String
  return self unless self =~ number 

  decimal, suffix = $2, $3

  self.sub!(number,'\1')

  coeff = case suffix
          when /G/ then 1073741824 
          when /M/ then 1048576 
          when /K/ then 1024
          end

  (coeff || 1) * (decimal ? self.to_f : self.to_i)
  
end