ruby - 为什么 fixnum 没有特征类?

标签 ruby eigenclass

该方法返回任意对象的特征类:

class Object
  def eigenclass
    class << self; self; end
  end
end

字符串示例:

"abc".eigenclass # => #<Class:#<String:0x331df0>>

数组:

[1, 2].eigenclass # => #<Class:#<Array:0x0000000065d218>>

但是对于 Fixnum:

1.eigenclass # => TypeError: can't define singleton

为什么?

最佳答案

作为Ruby Docs说:

There is effectively only one Fixnum object instance for any given integer value, so, for example, you cannot add a singleton method to a Fixnum.

BignumFloatSymbol 也是如此

关于ruby - 为什么 fixnum 没有特征类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39887145/

相关文章:

ruby-on-rails - 话语-Ruby 2.0/Rails 4?

ruby-on-rails - Encoding::UndefinedConversionError -ASCII-8BIT to UTF-8 ruby​​ on rails

Ruby - 有没有办法获取特征类的实例?

Ruby 本征类意外行为

ruby 1.8 与 1.9 : Determining whether a Ruby singleton class is a subclass of another class

ruby - 有没有办法在 Ruby 中解码 q 编码的字符串?

ruby - 可以告诉 ruby​​ 我是否在给定模块中定义了给定类

ruby - 动态地将模块中的方法添加到类的特定实例

ruby - Ruby 中的匿名类

javascript - Ruby 客户端 Eventbrite API 使用 Javascript 查找事件 ID