Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我希望能够为变量分配数字 2147483647,即最大 32 位有符号整数值。有什么方法可以从类中检索一个值,例如 Integer::MAX?
在 CRuby 2.5 和更新版本中,您可以执行以下操作:
require 'rbconfig/sizeof' RbConfig::LIMITS['INT32_MAX'] # => 2147483647
的键RbConfig::LIMITS是 C 限制宏的名称(请参阅和) <limits.h>,除了它是 Ruby 实现细节。<stdint.h><float.h>FIXNUM_*
RbConfig::LIMITS
<limits.h>
<stdint.h>
<float.h>
FIXNUM_*
不过,我建议为这些限制定义自己的常量(或局部变量)。它更便携。