2

这个方法是返回任何对象的特征类:

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

为什么?

4

1 回答 1

1

正如Ruby 文档所说:

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

对于Bignum,FloatSymbol

于 2016-10-06T07:22:29.863 回答