Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In Ruby, modules and classes are instances of the <a href="http://ruby-doc.org/core-1.9.3/Module.html"><code>Module</code></a> and <a href="http://ruby-doc.org/core-1.9.3/Class.html"><code>Class</code></a> classes, respectively. They derive their names from the <em>constant</em> they are assigned to. When you write:</p> <pre><code>class A::B # ... end </code></pre> <p>You are effectively writing:</p> <pre><code>A::B ||= Class.new do # ... end </code></pre> <p>Which is valid constant assignment syntax, and <em>assumes</em> that the <code>A</code> constant has been properly initialized <em>and</em> that it refers to a <code>Module</code> or a <code>Class</code>.</p> <p>For example, consider how classes are usually defined:</p> <pre><code>class A # ... end </code></pre> <p>What is effectively happening is this:</p> <pre><code>Object::A ||= Class.new do # ... end </code></pre> <p>Now, when you write:</p> <pre><code>class A class B # ... end end </code></pre> <p>What actually happens looks like this:</p> <pre><code>(Object::A ||= Class.new).class_eval do (A::B ||= Class.new).class_eval do # ... end end </code></pre> <p>Here's what is happening, in order:</p> <ol> <li>A new <code>Class</code> instance is asssigned to the <code>A</code> constant of <code>Object</code>, unless it was already initialized.</li> <li>A new <code>Class</code> instance is asssigned to the <code>B</code> constant of <code>A</code>, unless it was already initialized.</li> </ol> <p>This ensures the existence of <em>all</em> outer classes before attempting to define <em>any</em> inner classes.</p> <p>There is also a change in scope, which allows you to directly access <code>A</code>'s constants. Compare:</p> <pre><code>class A MESSAGE = "I'm here!" end # Scope of Object class A::B # Scope of B puts MESSAGE # NameError: uninitialized constant A::B::MESSAGE end # Scope of Object class A # Scope of A class B # Scope of B puts MESSAGE # I'm here! end end </code></pre> <p>According to <a href="http://yugui.jp/articles/846">this blog post</a>, the Ruby core team calls the "current class" the <code>cref</code>. Unfortunately, the author does not elaborate, but as he notes, it is separate from the context of <code>self</code>.</p> <hr> <p><a href="http://ruby2cext.rubyforge.org/limitations.html#section7">As explained here</a>, the <code>cref</code> is a <em>linked list</em> that represents the nesting of modules at some point in time.</p> <blockquote> <p>The current <code>cref</code> is used for constant and class variable lookup and for <code>def</code>, <code>undef</code> and <code>alias</code>.</p> </blockquote> <hr> <p>As the others have stated, they are different ways of expressing the same thing.</p> <p>There is, however, a subtle difference. When you write <code>class A::B</code>, you <em>assume</em> that the <code>A</code> class has already been defined. If it has not, you will get a <code>NameError</code> and <code>B</code> will not be defined at all.</p> <p>Writing properly nested modules:</p> <pre><code>class A class B end end </code></pre> <p>Ensures the <code>A</code> class exists before attempting to define <code>B</code>.</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