Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>(Answer to your comment)</p> <p><code>dog = 'dog'</code> or <code>String.new("dog")</code></p> <p>After dog = String.new, the field class of instance dog points to class String.</p> <pre><code>class &lt;&lt; dog puts "inside #{self}" #=&gt; inside #&lt;Class:#&lt;String:0x007fb38a83a820&gt;&gt; def bark puts 'woof' end end dog.bark #=&gt; "woof" p dog.singleton_methods #=&gt; ["bark"] </code></pre> <p>With <code>class &lt;&lt; dog</code> or <code>def dog.bark</code>, Ruby creates an anonymous class, the field class of instance dog now points to this anonymous class, and from there to String. Methods defined in this context with def or define_method go into the methods table of the anonymous class.</p> <p>Ruby 1.9.2 has introduced Object#singleton_class. [The Pickaxe] Returns the singleton class of obj, creating one if necessary. (I add) It is equivalent to <code>class &lt;&lt; self; self end</code>.</p> <p>The Ruby Programming Language (O'Reiily) simply says : to open the eigenclass [singleton class] of the object o, use class &lt;&lt; o.</p> <p>So I don't know how to read it loud. I have read that some would prefer <code>o &gt;&gt; class</code>. It's only recently that I have found how to figure out what this strange expression means. I pronounce : go from o to it's anonymous class.</p> <pre><code>class &lt;&lt; MyClass def dog puts 'dog as class method' end end MyClass.dog #=&gt; dog as class method </code></pre> <p>The same is true for a class. With <code>class MyClass</code>, MyClass, as instance of Class, is an object with a pointer to its class Class. With <code>def MyClass.some_method</code> or <code>class &lt;&lt; MyClass</code>, Ruby creates an anonymous class which is inserted between MyClass and Class, and class methods go into it.</p> <blockquote> <p>Maybe something like: "from class, instantiate singleton object self</p> </blockquote> <p>Yes for "from class/object" to anonymous singleton class/eigenclass/metaclass. But we don't instantiate self. Self (in Smaltalk, this in C++/Java) is kind of a reserved word which designate the receiver of the message. <code>dog.bark</code> : in OO language we say that the message bark in sent to object dog. Inside the method <code>bark</code>, self will be set to dog, so that we can reference dog. This is more obvious with </p> <pre><code>o1 = MyClass.new; o2 = MyClass.new o1.some_method; o2.some_method </code></pre> <p>some_method must be able to reference the receiver in a generic way, is it o1 or o2, this is what self is for.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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