Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby nested modules as namespaces
    text
    copied!<p>I have a nested structure of modules, which are used only for namespacing purposes; no mixing into classes etc. So I have a code like this:</p> <pre><code>module Lib module A def A.foo puts 'Lib::A::foo' end end module B def B.bar puts 'Lib::B::bar' end end end </code></pre> <p>Now suppose I want to add one more helper method in module <code>A</code>, and I want it to be used easily by both modules. I get the following:</p> <pre><code>module Lib module A def A.foo puts 'Lib::A::foo' end def A.helper puts 'Lib::A::helper' end end module B def B.bar puts 'Lib::B::bar' A::helper end end end </code></pre> <p>and it seems to work, but it has some drawback I'd like to get rid of:<br/> I don't want to call <code>helper</code> by its full qualified name (<code>A::helper</code>) all the time from inside <code>B</code>. I'd prefer to tell Ruby somehow that this namespace prefix is "default" and call it simply as <code>helper</code>. In C++ I could just write <code>using A::helper</code> inside namespace <code>B</code> and it would solve the problem. But how to do it in Ruby?</p> <p>I tried adding <code>include A</code> and <code>extend A</code> inside <code>B</code>, but none of them works. They seem to work only inside classes, when these modules are mixed in, but not when they're stand-alone, used just for namespacing purposes.</p> <p>Are there any other ways to make it work the way I want?</p> <p>Oh, and one more thing:<br/> Suppose a different scenario, where I want the <code>A::helper</code> to be used only from inside <code>A</code>'s methods, because it's just some implementation function, where I factored out some common code used by many functions inside <code>A</code>, but now I don't want it to be visible for the outside world, just to <code>A</code>'s methods. How can I do it?</p> <p>I tried with <code>module_function</code> + removing the <code>A.</code> prefix from all other functions which are supposed to be hidden, but then they're hidden also for other methods in <code>A</code>, because they're instance methods then, and modules cannot be instantiated. So how can I hide a module method from outside world and still allow it for the internal use by other module methods?</p> <p><strong>Edit</strong> Why downvoting? I tried to be as clear as I possibly can, that what I need is <strong>defaulting a namespace</strong> inside another namespace to <strong>get rid of long fully-qualified names altogether</strong> (not just aliasing them to something shorter), and that my problem doesn't concern classes &amp; objects at all, just plain modules used for namespacing purposes only. How else could I explain it?</p> <p>It's not my fault that namespacing mechanisms doesn't seem to be <em>fully</em> supported in Ruby <em>natively</em> as in languages such as C++, and that it seems to be just a <em>side-effect</em> of using modules and classes to have <em>some</em> functionality of true namespaces (they quack like ducks, they have ducky beaks, but they're platypuses, not ducks, and shouldn't be advertised as namespaces which they apparently aren't, because it only confuses people coming to Ruby from other languages with true namespaces), nor that you apparently don't understand concepts from other programming languages if they aren't easily possible in Ruby (since I see that you seem to pretend that my problem doesn't exist and revert back to what you <em>can</em> easily do in Ruby instead; but this is not what I need).</p> <p>And why the only answer which was relevant to my question has been deleted? Not cool ;.</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