Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112014-10-02T22:44:17ZRuby Issue Tracking System
Redmine Ruby master - Feature #10320 (Open): require into modulehttps://bugs.ruby-lang.org/issues/103202014-10-02T22:44:17Zsowieso (So Wieso)sowieso@dukun.de
<p>When requiring a library, global namespace always gets polluted, at least with one module name. So when requiring a gem with many dependencies, at least one constant enters global namespace per dependency, which can easily get out of hand (especially when gems are not enclosed in a module).</p>
<p>Would it be possible to extend require (and load, require_relative) to put all content into a custom module and not into global namespace?</p>
<p>Syntax ideas:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s1">'libfile'</span><span class="p">,</span> <span class="ss">into: :Lib</span> <span class="c1"># keyword-argument</span>
<span class="nb">require</span> <span class="s1">'libfile'</span> <span class="k">in</span> <span class="no">Lib</span> <span class="c1"># with keyword, also defining a module Lib at current binding (unless defined? Lib)</span>
<span class="n">require_qualified</span> <span class="s1">'libfile'</span><span class="p">,</span> <span class="ss">:Lib</span>
</code></pre>
<p>This would also make including code into libraries much easier, as it is well scoped.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">MyGem</span>
<span class="err"> </span><span class="nb">require</span> <span class="s1">'needed'</span> <span class="k">in</span> <span class="no">Need</span>
<span class="err"> </span><span class="k">def</span> <span class="nf">do_something</span>
<span class="err"> </span><span class="no">Need</span><span class="o">::</span><span class="n">important</span><span class="p">.</span><span class="nf">process!</span>
<span class="err"> </span><span class="k">end</span>
<span class="k">end</span>
<span class="c1"># library user is never concerned over needed's content</span>
</code></pre>
<p>Some problems to discuss:</p>
<ul>
<li>requiring into two different modules means loading the file twice?</li>
<li>monkeypatching libraries should only affect the module → auto refinements?</li>
<li>maybe also allow a binding as argument, not only a module?</li>
<li>privately require, so that required constants and methods are not accessible from the outside of a module (seems to difficult)</li>
<li>what about $global constants, read them from global scope but copy-write them only to local scope?</li>
</ul>
<p>Similar issue:<br>
<a href="https://bugs.ruby-lang.org/issues/5643" class="external">https://bugs.ruby-lang.org/issues/5643</a></p> Ruby master - Feature #9585 (Open): Add Object#in? to make ruby easier to readhttps://bugs.ruby-lang.org/issues/95852014-03-01T11:43:17Zsowieso (So Wieso)sowieso@dukun.de
<p>Please add an in? method to all objects, that allows the following:<br>
<code>4.in? 1,2,3,4 4.in? 1..4 "a".in? "abc" 7.in? 0..2, 5..8 3.in? small_numbers.select(&:odd?) =>true</code><br>
Background:<br>
Since my first steps in ruby it always bugs me, that using Array#include? to perform multiple checks in one line breaks symmetry, forces me to read backward and thus lets me stumble in the head. Ruby tries to be close to natural language, and therefore the subject should stand on the left side. Example:<br>
<code>if status == 1 if status == 1 or status == 2 if [1,2,127].include? status # breaks symmetry if status.in? 1, 2, 127 # better</code></p>
<p>Pros:<br>
‣ Nicer to read, no need to read the line backward (brings joy to writers and readers)<br>
‣ No new keyword<br>
‣ Breaks nothing</p>
<p>Cons:<br>
‣ One more method in Object<br>
‣ <code>"a".in? "abc", "def" vs "a".in? ["abc", "def"]</code> (implementation is yet an example)</p>
<p>Neutral:<br>
‣ Yet one more way to do it (isn't that ruby-style?)<br>
‣ Belongs to Object, as a comparison operator like ==, eql?, ===, nil?<br>
‣ “only cosmetics” vs elegance</p>
<p>Implementation for testing (you'd certainly find a less naive implementation):<br>
<code>class Object def in? *args raise ArgumentError if args.empty? args.any? {|a| a == self or a.include? self rescue false} end end</code><br>
This is related to <a href="https://bugs.ruby-lang.org/issues/3845" class="external">https://bugs.ruby-lang.org/issues/3845</a><br>
which was rejected for being an operator (even though the keyword already existed)</p> Ruby master - Feature #8987 (Open): map/collect extension which handles argumentshttps://bugs.ruby-lang.org/issues/89872013-10-05T23:02:50Zsowieso (So Wieso)sowieso@dukun.de
<p>Please consider extending map/collect by allowing additional arguments to be passed to proc, like:<br>
A: <code>[1,2,3,4].map :+, 4</code><br>
and/or<br>
B: <code>[1,2,3,4].map 4, &:+</code></p>
<p>=> [5, 6, 7, 8]</p>
<p>Variant A is probably more readable. Variant B is more versatile (allows arbitrary arguments to be passed to block).</p>