该方法返回任意对象的特征类:
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.
Bignum
、Float
和 Symbol
也是如此
关于ruby - 为什么 fixnum 没有特征类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39887145/