Why fixnum doesn't have eigenclass?

This method is to return eigenclass of any object:

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

Example for String:

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

Array:

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

But with Fixnum:

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

Why?


As the Ruby Docs say:

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.

The same is true for Bignum , Float and Symbol

链接地址: http://www.djcxy.com/p/95440.html

上一篇: 生成一个7位数的随机数

下一篇: 为什么fixnum没有特征类?