为什么我不能在Ruby中实例化整数类?

时间:2022-10-24 21:04:58

I thought all classes in Ruby can be instantiated. What's preventing Integer class from being instantiated with new method?

我认为Ruby中的所有类都可以实例化。是什么阻止了整型类被新方法实例化?

Integer.new
# => NoMethodError: undefined method `new' for Integer:Class

2 个解决方案

#1


7  

There are a few of those. Besides Integer, Float, and Symbol, you can't create a new instance of TrueClass, FalseClass and NilClass too.

有一些。除了整数、浮点数和符号之外,您还不能创建TrueClass、FalseClass和NilClass的新实例。

These classes (and their respective instances) are all special in Ruby and are handled in a specific way internally.

这些类(以及它们各自的实例)在Ruby中都是特殊的,并且在内部以特定的方式处理。

With small Integers for example, Ruby implicitly handles those. Instead of creating a new "actual" Ruby object for each integer number (which would be hugely wasteful), Ruby stores those as only their numeric value represented by the object_id. Thus, what you observe in Ruby as an instance of the Integer class is actually a single value im memory (more or less). To be able to pull this off, Ruby reserves all odd object_ids for integer values. Thus, the number 1 has the object_id of 3, the number 2 has the object_id of 5 and so on...

例如,对于小整数,Ruby隐式地处理它们。Ruby并没有为每个整数创建一个新的“实际的”Ruby对象(这将是非常浪费的),而是将它们存储为object_id表示的数字值。因此,在Ruby中作为Integer类实例观察到的实际上是一个单一值im内存(或多或少)。为了实现这一点,Ruby为整数值保留所有奇数object_id。因此,数字1的object_id为3,数字2的object_id为5等。

Due to this special handling by the Ruby language itself, you can't create a new Integer instance. Now given that Integers themselves are always immutable (that is, they can't be changed) they are only defined by their numeric value in the first place.

由于Ruby语言本身的特殊处理,您无法创建新的整数实例。考虑到整数本身总是不可变的(也就是说,它们不能被改变),它们首先是由它们的数值定义的。

(Note that this only works for small integers. For larger integers, depending on whether you are running on a 32 bit or 64 bit architecture, Ruby will still internally create real objects if the integer number can't fit into the scheme described above. This is however handled internally by Ruby and is basically an implementation detail of the language itself.)

(注意,这只适用于小整数。对于较大的整数,取决于您是在32位架构上运行还是在64位架构上运行,如果整数数字不能满足上面描述的方案,Ruby仍然会在内部创建真正的对象。但是这是Ruby内部处理的,基本上是语言本身的实现细节。

#2


3  

You can't allocate heap objects of an Integer in Ruby. In Ruby Integers are immediates which means you cannot have an instantiated version of the object. Since you can’t allocate them, you can’t create a subclass and allocate instances of the subclass.

不能在Ruby中分配整数的堆对象。在Ruby integer中,是即时性的,这意味着您不能拥有对象的实例化版本。因为不能分配它们,所以不能创建子类并分配子类的实例。

#1


7  

There are a few of those. Besides Integer, Float, and Symbol, you can't create a new instance of TrueClass, FalseClass and NilClass too.

有一些。除了整数、浮点数和符号之外,您还不能创建TrueClass、FalseClass和NilClass的新实例。

These classes (and their respective instances) are all special in Ruby and are handled in a specific way internally.

这些类(以及它们各自的实例)在Ruby中都是特殊的,并且在内部以特定的方式处理。

With small Integers for example, Ruby implicitly handles those. Instead of creating a new "actual" Ruby object for each integer number (which would be hugely wasteful), Ruby stores those as only their numeric value represented by the object_id. Thus, what you observe in Ruby as an instance of the Integer class is actually a single value im memory (more or less). To be able to pull this off, Ruby reserves all odd object_ids for integer values. Thus, the number 1 has the object_id of 3, the number 2 has the object_id of 5 and so on...

例如,对于小整数,Ruby隐式地处理它们。Ruby并没有为每个整数创建一个新的“实际的”Ruby对象(这将是非常浪费的),而是将它们存储为object_id表示的数字值。因此,在Ruby中作为Integer类实例观察到的实际上是一个单一值im内存(或多或少)。为了实现这一点,Ruby为整数值保留所有奇数object_id。因此,数字1的object_id为3,数字2的object_id为5等。

Due to this special handling by the Ruby language itself, you can't create a new Integer instance. Now given that Integers themselves are always immutable (that is, they can't be changed) they are only defined by their numeric value in the first place.

由于Ruby语言本身的特殊处理,您无法创建新的整数实例。考虑到整数本身总是不可变的(也就是说,它们不能被改变),它们首先是由它们的数值定义的。

(Note that this only works for small integers. For larger integers, depending on whether you are running on a 32 bit or 64 bit architecture, Ruby will still internally create real objects if the integer number can't fit into the scheme described above. This is however handled internally by Ruby and is basically an implementation detail of the language itself.)

(注意,这只适用于小整数。对于较大的整数,取决于您是在32位架构上运行还是在64位架构上运行,如果整数数字不能满足上面描述的方案,Ruby仍然会在内部创建真正的对象。但是这是Ruby内部处理的,基本上是语言本身的实现细节。

#2


3  

You can't allocate heap objects of an Integer in Ruby. In Ruby Integers are immediates which means you cannot have an instantiated version of the object. Since you can’t allocate them, you can’t create a subclass and allocate instances of the subclass.

不能在Ruby中分配整数的堆对象。在Ruby integer中,是即时性的,这意味着您不能拥有对象的实例化版本。因为不能分配它们,所以不能创建子类并分配子类的实例。