【问题标题】:Why fixnum doesn't have eigenclass?为什么fixnum没有特征类?
【发布时间】:2017-02-14 16:14:07
【问题描述】:

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

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

为什么?

【问题讨论】:

  • 我认为this 可能会对您有所帮助。请通过它。

标签: ruby eigenclass


【解决方案1】:

正如Ruby Docs 所说:

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

BignumFloatSymbol 也是如此

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-16
    • 2013-01-13
    • 2011-06-03
    • 2015-11-11
    相关资源
    最近更新 更多