Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Constant resolution always requires that you use <code>::</code>.</p> <p>Method invocation is idiomatically and usually a period (<code>.</code>), but <code>::</code> is also legal. This is not just true for so-called module methods, but for invoking any method on any object:</p> <pre><code>class Foo def bar puts "hi" end end Foo.new::bar #=&gt; hi </code></pre> <p>It's not so much "syntax sugar" as it is simply alternative syntax, such as the ability to write <code>if</code> or <code>case</code> statements with either a newline, <code>then</code> and newline, or just <code>then</code>.</p> <p>It is specifically allowed because Ruby allows methods with the same name as a constant, and sometimes it makes sense to think that they are the same item:</p> <pre><code>class Foo class Bar attr_accessor :x def initialize( x ) self.x = x end end def self.Bar( size ) Foo::Bar.new( size ) end end p Foo::Bar #=&gt; Foo::Bar (the class) p Foo::Bar(42) #=&gt; #&lt;Foo::Bar:0x2d54fc0 @x=42&gt; (instance result from method call) </code></pre> <p>You see this commonly in Ruby in the <a href="http://nokogiri.org" rel="nofollow noreferrer">Nokogiri</a> library, which has (for example) the <a href="http://nokogiri.org/Nokogiri/XML.html" rel="nofollow noreferrer"><code>Nokogiri::XML</code></a> module as well as the <a href="http://nokogiri.org/Nokogiri.html#method-c-XML" rel="nofollow noreferrer"><code>Nokogiri.XML</code></a> method. When creating an XML document, many people choose to write</p> <pre><code>@doc = Nokogiri::XML( my_xml ) </code></pre> <p>You see this also in the <a href="http://sequel.rubyforge.org" rel="nofollow noreferrer">Sequel</a> library, where you can write either:</p> <pre><code>class User &lt; Sequel::Model # Simple class inheritance class User &lt; Sequel::Model(DB[:regular_users]) # Set which table to use </code></pre> <p>Again, we have a <a href="http://sequel.rubyforge.org/rdoc/classes/Sequel.html#method-c-Model" rel="nofollow noreferrer">method (Sequel.Model)</a> named the same as a <a href="http://sequel.rubyforge.org/rdoc/classes/Sequel/Model.html" rel="nofollow noreferrer">constant (Sequel::Model)</a>. The second line could also be written as</p> <pre><code>class User &lt; Sequel.Model(DB[:regular_users]) </code></pre> <p>&hellip;but it doesn't look quite as nice.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. 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