Class: Gorillib::Factory::IntegerFactory

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

Overview

Note:

returns Bignum or Fixnum (instances of either are is_a?(Integer))

Converts arg to a Fixnum or Bignum.

  • Numeric types are converted directly, with floating point numbers being truncated
  • Strings are interpreted using Integer(), so: ** radix indicators (0, 0b, and 0x) are HONORED -- '011' means 9, not 11; '0x22' means 0, not 34 ** They must strictly conform to numeric representation or an error is raised (which differs from the behavior of String#to_i)
  • Non-string values will be converted using to_int, and to_i.

Examples:

IntegerFactory.receive(123.999)    #=> 123
IntegerFactory.receive(Time.new)   #=> 1204973019

IntegerFactory() handles floating-point numbers correctly (as opposed to Integer() and GraciousIntegerFactory)

IntegerFactory.receive("98.6")     #=> 98
IntegerFactory.receive("1234.5e3") #=> 1_234_500

IntegerFactory has love for your hexadecimal, and disturbingly considers 0-prefixed numbers to be octal.

IntegerFactory.receive("0x1a")     #=> 26
IntegerFactory.receive("011")      #=> 9

IntegerFactory() is not as gullible, or generous as GraciousIntegerFactory

IntegerFactory.receive("7eleven")  #=> (error)
IntegerFactory.receive("nonzero")  #=> (error)
IntegerFactory.receive("123_456L") #=> (error)

Direct Known Subclasses

BignumFactory, GraciousIntegerFactory

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



294
295
296
# File 'lib/gorillib/factories.rb', line 294

def convert(obj)
  Integer(obj)
end