Attaching a method to an object

Quoting from “Programming Ruby”, 4th edition, chapter III-22, “Method
Definition”:

"A definition using a method name of the form expr.methodname

creates a method associated with the object that is the value of the
expression; the method will be callable only by supplying the object
referenced by the expression as a receiver. This style of definition
creates per-object or singleton methods."

The following example is provided:

obj = Object.new
def obj.method # definition
end
obj.method # call

This example works well (Ruby 2.2.2). However if I do

x=7
def x.f
end

I get the error message

TypeError: can't define singleton

In what respect is my obj “obj” different from my object “x”, so that I
can attach methods to “obj”, but not to “x”?

Instances of Fixnum, like “7”, are singletons and are special.
For example: you might have two people with the same attributes who are
different, but 7 is the same number everywhere.
Try non-singleton object, like a more complex class, and it will work.

Joel P. wrote in post #1177616:

Instances of Fixnum, like “7”, are singletons and are special.

Thanks a lot. This explains it. Now that you mention it, I can see it in
Class: Fixnum (Ruby 2.2.0) ! …