Method: Hanami::Utils::Kernel.Float

Defined in:
lib/hanami/utils/kernel.rb

.Float(arg) ⇒ Float

Coerces the argument to be a Float.

It’s similar to Ruby’s Kernel.Float, but it doesn’t stop at the first error and raise an exception only when the argument can’t be coerced.

Examples:

Basic Usage

require 'bigdecimal'
require 'hanami/utils/kernel'

Hanami::Utils::Kernel.Float(1)                        # => 1.0
Hanami::Utils::Kernel.Float(1.2)                      # => 1.2
Hanami::Utils::Kernel.Float(011)                      # => 9.0
Hanami::Utils::Kernel.Float(0xf5)                     # => 245.0
Hanami::Utils::Kernel.Float("1")                      # => 1.0
Hanami::Utils::Kernel.Float(Rational(0.3))            # => 0.3
Hanami::Utils::Kernel.Float(Complex(0.3))             # => 0.3
Hanami::Utils::Kernel.Float(BigDecimal(12.00001))     # => 12.00001
Hanami::Utils::Kernel.Float(176605528590345446089)
  # => 176605528590345446089.0

Hanami::Utils::Kernel.Float(Time.now) # => 397750945.515169

Float Interface

require 'hanami/utils/kernel'

class Pi
  def to_f
    3.14
  end
end

pi = Pi.new
Hanami::Utils::Kernel.Float(pi) # => 3.14

Error Handling

require 'bigdecimal'
require 'hanami/utils/kernel'

# nil
Kernel.Float(nil)               # => TypeError
Hanami::Utils::Kernel.Float(nil) # => 0.0

# float represented as a string
Kernel.Float("23.4")               # => TypeError
Hanami::Utils::Kernel.Float("23.4") # => 23.4

# rational represented as a string
Kernel.Float("2/3")               # => TypeError
Hanami::Utils::Kernel.Float("2/3") # => 2.0

# complex represented as a string
Kernel.Float("2.5/1")               # => TypeError
Hanami::Utils::Kernel.Float("2.5/1") # => 2.5

# bigdecimal infinity
input = BigDecimal("Infinity")
Hanami::Utils::Kernel.Float(input) # => Infinity

# bigdecimal NaN
input = BigDecimal("NaN")
Hanami::Utils::Kernel.Float(input) # => NaN

Unchecked Exceptions

require 'date'
require 'bigdecimal'
require 'hanami/utils/kernel'

# Missing #to_f
input = OpenStruct.new(color: 'purple')
Hanami::Utils::Kernel.Float(input) # => TypeError

# When true
input = true
Hanami::Utils::Kernel.Float(input) # => TypeError

# When false
input = false
Hanami::Utils::Kernel.Float(input) # => TypeError

# When Date
input = Date.today
Hanami::Utils::Kernel.Float(input) # => TypeError

# When DateTime
input = DateTime.now
Hanami::Utils::Kernel.Float(input) # => TypeError

# Missing #nil?
input = BasicObject.new
Hanami::Utils::Kernel.Float(input) # => TypeError

# String that doesn't represent a float
input = 'hello'
Hanami::Utils::Kernel.Float(input) # => TypeError

# big rational
input = Rational(-8) ** Rational(1, 3)
Hanami::Utils::Kernel.Float(input) # => TypeError

# big complex represented as a string
input = Complex(2, 3)
Hanami::Utils::Kernel.Float(input) # => TypeError

Parameters:

  • arg (Object)

    the argument

Returns:

  • (Float)

    the result of the coercion

Raises:

  • (TypeError)

    if the argument can’t be coerced

See Also:

Since:

  • 0.1.1

[View source]

551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'lib/hanami/utils/kernel.rb', line 551

def self.Float(arg)
  super
rescue ArgumentError, TypeError
  begin
    case arg
    when NilClass, ->(a) { a.respond_to?(:to_f) && numeric?(a) }
      arg.to_f
    else
      raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float"
    end
  rescue NoMethodError
    raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float"
  end
rescue RangeError
  raise TypeError.new "can't convert #{inspect_type_error(arg)}into Float"
end