https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-05-08T10:29:17ZRuby Issue Tracking SystemRuby master - Feature #15837: Module#name_componentshttps://bugs.ruby-lang.org/issues/15837?journal_id=779562019-05-08T10:29:17Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>I was about to write a long reply, but I think it would become too long, so just a shorter comment.</p>
<p>I use something like this in a "base" gem that I tend to use. Example:</p>
<pre><code>class Foo
class Bar
NAMESPACE = inspect
</code></pre>
<p>And then I may split on the NAMESPACE constant, via .split('::'), in order to lateron display to the ruby user<br>
who is running the code either: the full name, or only a partial name (such as Bar:), or no name at all, on<br>
the commandline, including different colours (for example, if something went wrong, I tend to use red colour<br>
tone; and the leading "namespace" I tend to use with a light greyish colour if the terminal background is<br>
dark).</p>
<p>Example (even though I can not use colours here on the bug tracker):</p>
<pre><code>Foo:Bar: The network is currently unavailable; the dataset could not be obtained.
</code></pre>
<p>Or something like that.</p>
<p>This is especially useful to me in order to know when something has went wrong and I need to debug the<br>
situation at hand. I even have another project that e. g. accepts input such as "Foo::Bar" and then opens<br>
these .rb files in my editor, at the least for somewhat smaller classes. I sort of use ruby as an "IDE"<br>
here, strange as that may sound, but it is quite effective, actually.</p>
<p>I am not sure if I was able to explain that particular use case well; I try to not write too much. The use<br>
case is mostly to just display more information on the commandline, e. g. which .rb file is responsible for<br>
some particular output, and sometimes I may only show the name of the class rather than the full name,<br>
hence why I need .split('::') - but the more important aspect of this comment is that I can agree with<br>
mrkn that there may be common (or somewhat similar) use cases. Ruby users may have to work with .split('::')<br>
specifically, also as the example above showed.</p>
<p>I have no particular opinion on the name of the method itself; or what it should return (this is secondary to<br>
me) and I also can not answer the question as to whether this functionality may be sufficiently useful to other<br>
ruby users or not, and may thus encourage the addition of a new method (I have no strong opinion either way,<br>
neither about the name. I am sure if it would be approved then the name will be good, anyway), but I did want<br>
to describe one more use case, even if it is quite special and not that applicable to others.</p>
<p>To conclude - I agree with mrkn here; in my opinion it is somewhat common to see. There may also be more use<br>
cases; two I can possibly think of may be in rails/active* and another one possibly in introspection of ruby code,<br>
such as in pry. I actually looked at pry code and grep would display this:</p>
<pre><code>wrapped_module/candidate.rb: [/^\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/,
wrapped_module/candidate.rb: /^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/,
wrapped_module/candidate.rb: /^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/]
</code></pre>
<p>I think in particular the last two lines may help reinforce the sample listing by mrkn. It may not be a huge<br>
use case per se, but it may also not be totally uncommon in different projects, in my opinion. Thanks!</p>