Method: Emu.str_to_float

Defined in:
lib/emu.rb

.str_to_floatEmu::Decoder<Float>

Creates a decoder which converts a string to a float. It uses ++Float++ for the conversion.

Examples:

Emu.str_to_float.run!("42.2") # => 42.2
Emu.str_to_float.run!("42") # => 42.0
Emu.str_to_float.run!("a") # => raise DecodeError, "`\"a\"` can't be converted to a Float"
Emu.str_to_float.run!(42) # => raise DecodeError, "`42` is not a String"

Returns:



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/emu.rb', line 50

def self.str_to_float
  Decoder.new do |s|
    next Err.new("`#{s.inspect}` is not a String") unless s.is_a?(String)

    begin
      Ok.new(Float(s))
    rescue TypeError, ArgumentError
      Err.new("`#{s.inspect}` can't be converted to a Float")
    end
  end
end