class Gorillib::Factory::IntegerFactory
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.
@example
IntegerFactory.receive(123.999) #=> 123 IntegerFactory.receive(Time.new) #=> 1204973019
@example IntegerFactory() handles floating-point numbers correctly (as opposed to ‘Integer()` and GraciousIntegerFactory
)
IntegerFactory.receive("98.6") #=> 98 IntegerFactory.receive("1234.5e3") #=> 1_234_500
@example IntegerFactory
has love for your hexadecimal, and disturbingly considers 0-prefixed numbers to be octal.
IntegerFactory.receive("0x1a") #=> 26 IntegerFactory.receive("011") #=> 9
@example IntegerFactory() is not as gullible, or generous as GraciousIntegerFactory
IntegerFactory.receive("7eleven") #=> (error) IntegerFactory.receive("nonzero") #=> (error) IntegerFactory.receive("123_456L") #=> (error)
@note returns Bignum or Fixnum (instances of either are ‘is_a?(Integer)`)
Public Instance Methods
# File lib/gorillib/model/factories.rb, line 286 def convert(obj) Integer(obj) end