class Gorillib::Factory::GraciousIntegerFactory

Converts arg to a Fixnum or Bignum.

@example

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

@example GraciousIntegerFactory quietly mangles your floating-pointish strings

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

@example GraciousIntegerFactory does not care for your hexadecimal

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

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

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

@note returns Bignum or Fixnum (instances of either are ‘is_a?(Integer)`)

Public Instance Methods

convert(obj) click to toggle source

See examples/benchmark before ‘improving’ this method.

# File lib/gorillib/model/factories.rb, line 321
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