How can I assign a variable the largest 32 bit signed value in ruby without explicitly defining it

1.3k Views Asked by At

I want to be able to assign a variable the number 2147483647, the maximum 32-bit signed integer value. Is there some way that I can retrieve a value from the class, such as Integer::MAX?

1

There are 1 best solutions below

0
cremno On

In CRuby 2.5 and newer you can do the following:

require 'rbconfig/sizeof'
RbConfig::LIMITS['INT32_MAX']  # => 2147483647

The keys of RbConfig::LIMITS are the names of C limit macros (see <limits.h>, <stdint.h> and <float.h>) except FIXNUM_* which is a Ruby implementation detail.

I recommend defining your own constant (or local variable) for these limits though. It's more portable.