Class: Gorillib::Factory::FloatFactory

Inherits:
ConvertingFactory show all
Defined in:
lib/gorillib/factories.rb

Overview

Returns arg converted to a float.

  • Numeric types are converted directly
  • Strings strictly conform to numeric representation or an error is raised (which differs from the behavior of String#to_f)
  • Strings in radix format (an exact hexadecimal encoding of a number) are properly interpreted.
  • Octal is not interpreted! This means an IntegerFactory receiving '011' will get 9, a FloatFactory 11.0
  • Other types are converted using obj.to_f.

Examples:

FloatFactory.receive(1)                     #=> 1.0
FloatFactory.receive("123.456")             #=> 123.456
FloatFactory.receive("0x1.999999999999ap-4" #=> 0.1

FloatFactory is strict in some cases where GraciousFloatFactory is not

FloatFactory.receive("1_23e9f")             #=> (error)

FloatFactory() is not as gullible as GraciousFloatFactory

FloatFactory.receive("7eleven")             #=> (error)
FloatFactory.receive("nonzero")             #=> (error)

Direct Known Subclasses

GraciousFloatFactory

Instance Method Summary collapse

Methods inherited from ConvertingFactory

#receive

Methods inherited from BaseFactory

blankish?, #blankish?, #initialize, #native?, native?, #receive, typename, #typename

Constructor Details

This class inherits a constructor from Gorillib::Factory::BaseFactory

Instance Method Details

#convert(obj) ⇒ Object



369
# File 'lib/gorillib/factories.rb', line 369

def convert(obj) Float(obj) ; end