Class: Gorillib::Factory::GraciousFloatFactory

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

Overview

Returns arg converted to a float.

  • Numeric types are converted directly
  • Strings can have ',' (which are removed) or end in /LlFf/ (pig format); they should other conform to numeric representation or an error is raised. (this 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:

GraciousFloatFactory.receive(1)                     #=> 1.0
GraciousFloatFactory.receive("123.456")             #=> 123.456
GraciousFloatFactory.receive("0x1.999999999999ap-4" #=> 0.1
GraciousFloatFactory.receive("1_234.5")             #=> 1234.5

GraciousFloatFactory is generous in some cases where FloatFactory is not

GraciousFloatFactory.receive("1234.5f")             #=> 1234.5
GraciousFloatFactory.receive("1,234.5")             #=> 1234.5
GraciousFloatFactory.receive("1234L")               #=> 1234.0

GraciousFloatFactory is not as gullible as #to_f

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

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



399
400
401
402
# File 'lib/gorillib/factories.rb', line 399

def convert(obj)
  if String === obj then obj = obj.to_s.tr(FLT_CRUFT_CHARS,'') ; end
  super(obj)
end