为什么fixnum没有本征类?

时间:2016-10-06 03:41:19

标签: 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

为什么?

1 个答案:

答案 0 :(得分:1)

正如Ruby Docs所说:

  

对于任何给定的整数值,实际上只有一个Fixnum对象实例,因此,例如,您不能将单例方法添加到Fixnum。

BignumFloatSymbol

也是如此