The behavior of an instance is determined by the class, but we know that a particular instance should have special behavior. In most languages, we must make another class for the instance, while in ruby we can append methods to a paticular instance without such tiresomes.
ruby> class SingletonTest ruby| def size ruby| print "25\n" ruby| end ruby| end nil ruby> test1 = SingletonTest.new #<SingletonTest:0xbc468> ruby> test2 = SingletonTest.new #<SingletonTest:0xbae20> ruby> def test2.size ruby| print "10\n" ruby| end nil ruby> test1.size 25 nil ruby> test2.size 10 nil
In this example, test1 and test2 belong to same class, though, test2 has a redefinition of the `size' method and they behave differently. Such a method of a particular instance is called `singleton method'.
The singleton methods are used in the cases of, for example, buttons of GUI, which has different action for the press event. In this case, we may redefine the action suitably for each button object.
The concept of the singleton method is not original to ruby, as it appears in few other languages other than ruby, such as CLOS, Dylan, etc. Also, some languages, for example, Self and NewtonScript, have the singleton methods only. Such are called `prototype-base' languages.