Class: Gorillib::Factory::GraciousIntegerFactory

Inherits:
IntegerFactory 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 #to_i, so: ** radix indicators (0, 0b, and 0x) are IGNORED -- '011' means 11, not 9; '0x22' means 0, not 34 ** Strings will be very generously interpreted
  • Non-string values will be converted using to_i

Examples:

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

GraciousIntegerFactory quietly mangles your floating-pointish strings

GraciousIntegerFactory.receive("123.4e-3") #=> 123
GraciousIntegerFactory.receive("1e9")      #=> 1

GraciousIntegerFactory does not care for your hexadecimal

GraciousIntegerFactory.receive("0x1a")     #=> 0
GraciousIntegerFactory.receive("011")      #=> 11

GraciousIntegerFactory is generous (perhaps too generous) where IntegerFactory() is not

GraciousIntegerFactory.receive("123_456L") #=> 123_456
GraciousIntegerFactory.receive("7eleven")  #=> 7
GraciousIntegerFactory.receive("nonzero")  #=> 0

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

See examples/benchmark before 'improving' this method.



329
330
331
332
333
334
335
# File 'lib/gorillib/factories.rb', line 329

def convert(obj)
  if ::String === obj then
    obj = obj.to_s.tr(::Gorillib::Factory::FLT_CRUFT_CHARS, '') ;
    obj = ::Kernel::Float(obj) if ::Gorillib::Factory::FLT_NOT_INT_RE === obj ;
  end
  ::Kernel::Integer(obj)
end