Harsh Harsh - 1 year ago 89
Ruby Question

Integer.MIN_VALUE equivalent in Ruby

Java has a constant

that holds the minimum value an Integer can hold.

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

Answer Source

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