Why fixnum doesn't have eigenclass?

55 Views Asked by At

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?

1

There are 1 best solutions below

0
Santosh Sharma On

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