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”?