Class: Numeric

Inherits:
Object
  • Object
show all
Defined in:
lib/arbi/utils/numeric.rb

Overview

– Copyleft shura. [ [email protected] ]

This file is part of arbi.

arbi is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

arbi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with arbi. If not, see <www.gnu.org/licenses/>. ++

Defined Under Namespace

Modules: Scalar

Class Method Summary collapse

Class Method Details

.parse(str) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/arbi/utils/numeric.rb', line 44

def self.parse(str)
  if str.strip.match(/^([^\s]+)\s+(#{Numeric::Scalar::MULTIPLIERS.keys.map {|x|
        Regexp.escape(x)
        }.join(?|)})?(.*?)$/)
    num, mul, unit = $1, $2, $3
    v = begin
      Integer(num)
    rescue ArgumentError
      Float(num) rescue nil
    end

    return nil unless v

    if Numeric::Scalar::MULTIPLIERS[mul]
      v = (v * Numeric::Scalar::MULTIPLIERS[mul]).to_f
    end

    if !unit.empty?
      v.unit = unit.dup
    end

    return v
  end
end