class Gorillib::Factory::GraciousIntegerFactory
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
@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/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