What is the difference between Integer and Fixnum?

I know that the Fixnum class inherits from the Integer class. But what is the actual difference between the two? Are there any use cases when we sometimes use Fixnum and sometimes use Integer instead?

+45
ruby
Jan 27 '14 at 3:40
source share
2 answers

You never "use" Integer . This is an abstract class whose task is to provide methods for its children ( Fixnum and Bignum ). Under no circumstances will you ever ask for an object class and say that it is an Integer .

+32
Jan 27 '14 at 3:47
source share

UPDATE : With Ruby 2.4, the Fixnum and Bignum have disappeared, there is only Integer . Exact optimizations still exist, but they are considered as “correct” compiler optimizers, i.e. Behind the scenes, invisible to the programmer.




This is somewhat confusing. Integer is the real class you should think about. Fixnum is basically a performance optimization that should never have been seen by a programmer in the first place. (Compare this to flonums in YARV, which are implemented entirely as optimizations within the VM and are never exposed to the programmer.)

Basically, Fixnum fast and Bignum is slow (er), and the implementation automatically switches between them. You never ask one of them directly, you just get one or the other, depending on whether your integer fits a limited size Fixnum or not.

+42
Jan 28 '14 at 16:28
source share



All Articles