Name
def Statement for Singleton Methods
Synopsis
defreceiver.method
([arg
...,arg
=default
..., *arg
, &arg
])code
[rescue [exception_class
[,exception_class
...]] [=>variable
] [then]code
]... [elsecode
] [ensurecode
] end
Tip
A period
.
after
receiver
can be replaced by two colons
(::)
. They work the same way, but
::
is often used for class methods.
A restriction in the implementation of
Ruby prevents the definition of singleton methods associated with
instances of the Fixnum
or
Symbol
class.
a = "foo" def a.foo printf "%s(%d)\n", self, self.size end a.foo # "foo" is available for a only
Get Ruby in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.