<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Mon, 15 Jun 2015 at 14:10 Carlos Alonso <<a href="mailto:info@mrcalonso.com">info@mrcalonso.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">+1 for 1.</div><div class="gmail_quote"><br></div><div class="gmail_quote">And on that same topic I have a question:</div><div class="gmail_quote"><br></div><div class="gmail_quote">Basically, what's the difference between this code:</div><div class="gmail_quote"><br></div><div class="gmail_quote">module A</div><div class="gmail_quote">  class B</div><div class="gmail_quote">  end</div><div class="gmail_quote">end</div><div class="gmail_quote"><br></div><div class="gmail_quote">and </div><div class="gmail_quote"><br></div><div class="gmail_quote">class A::B</div><div class="gmail_quote"><br></div><div class="gmail_quote">end</div><div class="gmail_quote"><br></div><div class="gmail_quote">I've spotted the difference that the second class could not access any constant defined within module A, but what's the point of that second syntax? When is it recommended over the above one?</div></div></div></blockquote><div><br></div><div>:: is the constant lookup operator. It looks-up constants defined within other constants. This is so that you can gain access to constants which have been name-spaced.</div><div><br></div><div>In ruby, all class and module names are constants - which have been assigned an instance of the Class or Module class.</div><div><br></div><div>In your example above, initially, you have defined the class B inside the module A. At this point the lexical scope would allow you to refer to any constants defined inside module B without referring to B and using the :: operator.</div><div><br></div><div>Later you reopen the class A::B (which is its full name-spaced name) directly, but now the lexical scope of the code is not within the module A. This is why you would not have access to any other constants defined inside the module A.</div><div><br></div><div>If you try this code:</div><div><br></div><div>class C::D<br></div><div>end</div><div><br></div><div><div>You should see it raise a NameError as there is no constant C defined, you would have to have already declared a C module or constant:</div></div><div><br></div><div>class C</div><div>end</div><div><br></div><div>class C::D</div><div>end</div><div><br></div><div>I'm not sure of a good reason to use this form when defining classes? I wonder if it's a quirk of how the :: operator works, rather than having any specific use case?</div><div><br></div><div>I suppose there could be a use-case when you specifically want to avoid being in the same lexical scope, but I can't think of a good one and can imagine it causing confusion later on.</div><div><br></div><div>Steve</div><div> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">Cheers!</div></div></div>
_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org" target="_blank">Chat@lists.lrug.org</a><br>
Archives: <a href="http://lists.lrug.org/pipermail/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/pipermail/chat-lrug.org</a><br>
Manage your subscription: <a href="http://lists.lrug.org/options.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/options.cgi/chat-lrug.org</a><br>
List info: <a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" rel="noreferrer" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
</blockquote></div></div>