Module: Svgcode::Utility

Defined in:
lib/svgcode/utility.rb

Constant Summary collapse

SCI_SEP =
/e/i

Class Method Summary collapse

Class Method Details

.x_to_f(x) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/svgcode/utility.rb', line 5

def self.x_to_f(x)
  f =
  if x.is_a?(Numeric)
    x.to_f
  elsif x.is_a?(String)
    if x.match(SCI_SEP)
      parts = x.split(SCI_SEP)

      unless parts.length == 2
        raise ArgumentError.new('x is an unknown number format')
      end

      parts.first.to_f * 10.0 ** parts.last.to_f
    else
      x.to_f
    end
  else
    raise ArgumentError.new('x must be a Numeric or a String')
  end

  f
end