简体   繁体   English

12345.class在Ruby中返回'Integer'而不是'Fixnum'

[英]12345.class returning 'Integer' not 'Fixnum' in Ruby

On the Ruby tutorials I'm watching when you enter a number with .class it returns either Bignum or Fixnum . 在Ruby教程中我正在观察当你输入一个带.class的数字时,它会返回BignumFixnum

When I do this I get Integer instead: 当我这样做时,我得到了Integer

12345.class
#=> Integer

I'm using irb in the command line on a mac. 我在mac上的命令行中使用irb。 Why does the above not return Fixnum like it does in the tutorials? 为什么上面没有像教程中那样返回Fixnum

It depends on the Ruby version. 这取决于Ruby版本。 From Ruby 2.4.0 we have just Integer s, no more Fixnum s and Bignum s distinction 从Ruby 2.4.0我们只有Integer s,没有FixnumBignum的区别

https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/ https://www.ruby-lang.org/en/news/2016/12/25/ruby-2-4-0-released/

You should get a better tutorial. 你应该得到一个更好的教程。

Ruby only guarantees that a class named Integer exists. Ruby 保证名为Integer的类存在。 It allows implementation-specific subclasses of Integer , and it mandates that if implementation-specific subclasses do exist, then Integer must be fully abstract, but it does not force those implementation-specific subclasses to exist. 允许实现特定的子类Integer ,并且它要求如果 确实存在实现特定的子类,那么Integer 必须是完全抽象的,但它并没有强迫那些实现特定的子类存在。

I other words: 我换句话说:

  • Integer always exists. Integer始终存在。
  • Implementation-specific subclasses of Integer may or may not exist. 特定于实现的Integer子类可能存在也可能不存在。
  • Those implementation-specific subclasses may or may not be named Bignum and Fixnum . 那些特定于实现的子类可能会或可能不会被命名为BignumFixnum

The tutorial you are using is simply wrong (or at least outdated). 您使用的教程完全错误(或至少过时)。 This has been officially documented at least since the very first draft of the ISO Ruby Language Specification in 2009, but has been the case even before that. 至少自2009年ISO Ruby语言规范的初稿以来,这已被正式记录,但在此之前就已如此。

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

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