Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The <code>class &lt;&lt; self</code> notation opens up the eigenclass of an object. An eigenclass is an anonymous class that stores instance-specific behaviour. In the case of a class an eigenclass is sometimes called a metaclass. </p> <p>Ruby uses eigenclasses to implement so called 'class methods' (also called static methods).</p> <p>A Class (as moritz stated) is also an Object in Ruby and in so far as it is an object it also has a class. The class of a class in Ruby is called <code>Class</code>.</p> <p>A 'class method' in any language is a method in which a class is the receiver - that is the method is directly invoked on the class itself. </p> <p>However in order for a method to be invoked on a receiver that method must be defined on the class of that receiver. In the case of classes a 'class method' <strong>could</strong> be implemented as an instance method on the <code>Class</code> class.</p> <p>But defining an instance method on <code>Class</code> would mean that ALL classes get access to that class method which is not ideal. </p> <p>Enter the eigenclass, as stated before, the eigenclass for an object is a special class that stores the methods unique to that object. In the case of classes the eigenclass subclasses the <code>Class</code> class and is the direct class of the class.</p> <p>'class methods' in Ruby therefore are just 'instance methods' defined on the class's eigenclass.</p> <p>The <code>def MyClass.my_method</code> notation actually defines <code>my_method</code> on the eigenclass of MyClass. If you use this notation you can get by (for a while) without actually understanding eigenclasses since you can trick yourself into thinking it is just Ruby's way of defining 'static methods' and continue thinking Ruby's class model is similar to Java's. However, the <code>class &lt;&lt; self</code> notation allows no such interpretation and you must come to terms with the reality of eigenclasses.</p> <p>In summary, 'class methods' are actually 'instance methods' defined on the eigenclass and the <code>class &lt;&lt; self</code> gives you access to the eigenclass.</p> <p>For more reading check out these links:</p> <p><a href="http://banisterfiend.wordpress.com/2008/11/25/a-complete-ruby-class-diagram/" rel="noreferrer">http://banisterfiend.wordpress.com/2008/11/25/a-complete-ruby-class-diagram/</a></p> <p><a href="http://banisterfiend.wordpress.com/2008/10/25/the-secret-life-of-singletons/" rel="noreferrer">http://banisterfiend.wordpress.com/2008/10/25/the-secret-life-of-singletons/</a></p> <p><a href="http://www.klankboomklang.com/2007/09/21/the-singleton-class/" rel="noreferrer">http://www.klankboomklang.com/2007/09/21/the-singleton-class/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload