1 # depends on: class.rb numeric.rb ctype.rb precision.rb
3 class Integer < Numeric
7 def self.induced_from(obj)
14 raise TypeError, "failed to convert #{obj.class} into Integer"
19 self & Type.coerce_to(other, Integer, :to_int)
23 self | Type.coerce_to(other, Integer, :to_int)
27 self ^ Type.coerce_to(other, Integer, :to_int)
60 alias_method :to_int, :to_i
61 alias_method :round, :to_i
62 alias_method :truncate, :to_i
65 raise RangeError.new("#{self} is out of the valid character range") if self > 255 || self < 0
66 String.template 1, self
70 return 0 if index.is_a?(Bignum)
71 index = Type.coerce_to(index, Integer, :to_int)
72 index < 0 ? 0 : (self >> index) & 1
76 if !exp.is_a?(Integer)
77 b, a = math_coerce(exp)
82 return self if exp == 1 || self == 0 || self == 1
83 return exp % 2 == 0 ? 1 : -1 if self == -1
86 Float(self) ** Float(exp)
106 alias_method :succ, :next
113 # Returns the minimum number of bits required for integer in (signed int)
116 # NOTE: rshift would probably be slightly more efficient but since i'm
117 # probably going to use this to simplify the complex behavior of
118 # ruby's << and >> it would defeat the purpose by creating a circular
121 # TODO: convert algorithm to primitive so no circular dependency?
128 bits(int / 2) + 1 # could use >> in primitive
130 bits(~int / 2) + 1 # could use >> in primitive