简体   繁体   English

红宝石Fixnum类的大小函数

[英]Size function for ruby Fixnum class

Why the ruby size function for the number returning output like this? 为什么红宝石尺寸函数返回这样的数字输出?

1000000.size              # => 8
99999999999999999999.size # => 9

Ruby internally uses two different structures for storing integers - Fixnum and Bignum . Ruby内部使用两种不同的结构来存储整数FixnumBignum The first one is used for smaller number and maps directly to a long integer on the host architecture. 第一个用于较小的数字,并直接映射到主机体系结构上的长整数。 It's faster and constant in size, but limited on how much it can store. 它的速度更快且大小不变,但存储量有限。 The latter is used for storring arbitrarily large numbers and its size depends on how big the number is. 后者用于存储任意大的数字,其大小取决于数字的大小。

1000000.class              # => Fixnum
99999999999999999999.class # => Bignum

The conversion is done internally. 转换在内部完成。 In fact with ruby 2.4.0, there will be only one class - Integer . 实际上,在ruby 2.4.0中,只有一个类Integer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM