|
"http://www.w3.org/TR/REC-html40/strict.dtd">
The behavior of an instance is determined by its class, but there may be times we know that a particular instance should have special behavior. In most languages, we must go to the trouble of defining another class, which would then only be instantiated once. In ruby we can give any object its own methods.
In this example, Singleton methods are often used for elements of a graphic user interface (GUI), where different actions need to be taken when different buttons are pressed. Singleton methods are not unique to ruby, as they appear in CLOS, Dylan, etc. Also, some languages, for example, Self and NewtonScript, have singleton methods only. These are sometimes called prototype-based languages.
|