Harsh Harsh - 5 months ago 23
Ruby Question

Integer.MIN_VALUE equivalent in Ruby

Java has a constant

Integer.MIN_VALUE
that holds the minimum value an Integer can hold.

Is there any equivalent code to represent the minimum Integer value in Ruby language?

Answer

Ruby automatically "upgrades" the number (FixNumb -> BigNum) to hold the value provided, but the minimal value before the upgrade is:

FIXNUM_MIN = -(2**(0.size * 8 -2))

0.size gives the number of bytes used to store the number, so we need to multiply it by 8, to get bits and substract 2 bits.

2 bits is for:

  • one bit for sign
  • one bit for marking the number as integer