In Ruby a class itself is an instance of the Ruby class. AnyClassName.class => Class
Objects can also have methods that are independent of the parent class definition. They are called singleton methods and are stored on the metaclass of the object.When you declare a singleton method on an object, Ruby automatically creates a class to hold just that method. This class is called the ‘metaclass’ of the object.Whenever you send a message to the object, it first looks to see whether the method exists in its metaclass. If it is not there, it gets propagated to the actual class of the object and if it is not found there, the message traverses the inheritance hierarchy.
Ruby classes themselves have their own singleton classes since classes are objects as well. Basically these type of methods belong to a single object rather than to an entire class and other objects.The class « idiom is simply Ruby’s syntax for accessing the scope of an object’s singleton(meta) class.
We are defining methods particular to an object.It would be defined in it’s metaclass. For getting all singleton method for an object use:
Use of « :
For defining metaclasses or scoping methods for a particular metaclass.comments powered by Disqus