[英]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
的数字时,它会返回Bignum
或Fixnum
。
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,没有Fixnum
和Bignum
的区别
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
始终存在。 Integer
may or may not exist. Integer
子类可能存在也可能不存在。 Bignum
and Fixnum
. Bignum
和Fixnum
。 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.