Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112022-05-09T07:44:14ZRuby Issue Tracking System
Redmine Ruby master - Bug #18765 (Closed): Wrong description introduced by https://github.com/ruby/ruby/p...https://bugs.ruby-lang.org/issues/187652022-05-09T07:44:14Zsawa (Tsuyoshi Sawada)
<p>Commit <a href="https://github.com/ruby/ruby/pull/4938/files" class="external">https://github.com/ruby/ruby/pull/4938/files</a> introduced problems and made the description wrong.</p>
<ol>
<li>
<p>For methods <code>slice_after</code> and <code>slice_when</code>, it introduces expressions like "partition elements into arrays ('slices')", and for method <code>chunk_while</code>, "partition elements into arrays ('chunks')". It suggests to call the resulting elements using different words depending on the name of the method. But that does not make sense. They are all simply arrays, and there is no need to distinguish a "slice array" from a "chunk array". (They can all be called an "array", "slice", or "chunk", or whatever.)</p>
<p>Perhaps, it is attempting to explain where the method names came from, under the assumption that the words "slice" and "chunk" in these method names are nouns. If so, that is wrong. The succeeding parts "when (block)" and "while (block)" are (to correspond to English) adverbial clauses, not adjectival (relative) clauses; hence, these "slice" and "chunk" must be interpreted as verbs, not nouns. In fact, "a slice when y is not a successor of x" or "a chunk while y is a successor of x" does not make sense, whereas "slice it when y is not a successor of x" and "chunk them while y is a successor of x" do make sense.</p>
<p>The difference between the "slice" and "chunk" methods lies in the process, not the return value. If you want to use these words, it can be something like "slice the receiver into arrays when the block returns a truthy value" and "chunk the elements together while the block returns a truthy value".</p>
</li>
<li>
<p>In the description of <code>slice_when</code> and <code>chunk_while</code>, it says "it calls the block with each element and its successor", but that is not true. If you are going to phrase it that way, then it only calls each element except for the last one.</p>
</li>
<li>
<p>In the description of <code>slice_when</code>, it says "begins a new slice if and only if the block returns a truthy value", but that is not true. Regardless of the value of the block, the first element always begins a new "slice".</p>
</li>
<li>
<p>Most crucially, in the description of <code>chunk_while</code>, the expression "begins a new chunk if and only if the block returns a truthy value" is entirely wrong.</p>
</li>
</ol> Ruby master - Bug #16842 (Closed): `inspect` prints the UTF-8 character U+0085 (NEXT LINE) verbat...https://bugs.ruby-lang.org/issues/168422020-05-09T14:35:11Zsawa (Tsuyoshi Sawada)
<p>The UTF-8 character U+0085 (NEXT LINE) is not printable, but <code>inspect</code> prints the character verbatim (within double quotation):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mh">0x85</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">).</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="mh">0x85</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">).</span><span class="nf">inspect</span>
<span class="c1">#=> "\"</span>
<span class="p">\</span><span class="s2">""</span>
</code></pre>
<p>My understanding is that non-printable characters are not printed verbatim with <code>inspect</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1">#=> "\"\\n\""</span>
</code></pre>
<p>while printable characters are:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"a"</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="s2">"a"</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1"># => "\"a\""</span>
</code></pre>
<p>I ran the following script, and found that U+0085 is the only character within the range U+0000 to U+FFFF that behaves like this.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">verbatim?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="o">!</span><span class="n">char</span><span class="p">.</span><span class="nf">inspect</span><span class="p">.</span><span class="nf">start_with?</span><span class="p">(</span><span class="sr">%r{</span><span class="se">\"\\</span><span class="sr">[a-z]}</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">printable?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="n">char</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span>
<span class="k">end</span>
<span class="p">(</span><span class="mh">0x0000</span><span class="o">..</span><span class="mh">0xffff</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
<span class="k">begin</span>
<span class="n">char</span> <span class="o">=</span> <span class="n">i</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">)</span>
<span class="k">rescue</span> <span class="no">RangeError</span>
<span class="k">next</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s1">'%#x'</span> <span class="o">%</span> <span class="n">i</span> <span class="k">unless</span> <span class="n">verbatim?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span> <span class="o">==</span> <span class="n">printable?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="k">end</span>
</code></pre> Ruby master - Bug #16599 (Closed): did_you_mean is not activated for NameError and KeyErrorhttps://bugs.ruby-lang.org/issues/165992020-01-28T20:26:23Zsawa (Tsuyoshi Sawada)
<p><a href="https://www.rubydoc.info/gems/did_you_mean/" class="external">This document</a> claims that the did_you_mean gem responds to NameError, NoMethodError, and KeyError, but it actually seems to only respond to NoMethodError.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="p">()</span>
<span class="c1"># >> NoMethodError: undefined method `foo' for main:Object</span>
<span class="c1"># >> Did you mean? for</span>
<span class="n">foo</span>
<span class="c1"># >> NameError: undefined local variable or method `foo' for main:Object</span>
<span class="no">Foo</span>
<span class="c1"># >> NameError: uninitialized constant Foo</span>
<span class="p">{</span><span class="ss">foo: </span><span class="mi">1</span><span class="p">}.</span><span class="nf">fetch</span><span class="p">(</span><span class="ss">:bar</span><span class="p">)</span>
<span class="c1"># >> KeyError: key not found: :bar</span>
</code></pre> Ruby master - Feature #15919 (Rejected): Offset parameter for `Integer#times`https://bugs.ruby-lang.org/issues/159192019-06-13T09:18:47Zsawa (Tsuyoshi Sawada)
<p>I request an optional argument on <code>Integer#times</code> to set the offset at start, just like <code>Enumerator#with_index</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">5</span><span class="p">.</span><span class="nf">times</span><span class="p">(</span><span class="mi">3</span><span class="p">){</span><span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="nb">p</span> <span class="n">i</span><span class="p">}</span>
<span class="c1"># >> 3</span>
<span class="c1"># >> 4</span>
<span class="c1"># >> 5</span>
<span class="c1"># >> 6</span>
<span class="c1"># >> 7</span>
</code></pre>
<p>I think there are plenty of use cases, especially when the offset is 1.</p> Ruby master - Bug #15708 (Rejected): Implicit numbered argument decomposes an arrayhttps://bugs.ruby-lang.org/issues/157082019-03-19T11:08:39Zsawa (Tsuyoshi Sawada)
<p>In the following, <code>@1</code> refers to the entire item iterated:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="err">@</span><span class="mi">1</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
</code></pre>
<p>whereas in the following, <code>@1</code> refers to the first item achieved by decomposing the item iterated, behaving the same as <code>x</code> given by <code>|(x)|</code> rather than by <code>|x|</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">]]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [[1], [2], [3]]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="err">@</span><span class="mi">1</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
</code></pre>
<p>Is this intended?</p> Ruby master - Feature #15589 (Closed): `Numeric#zero?` is much slower than `== 0`https://bugs.ruby-lang.org/issues/155892019-02-06T09:34:41Zsawa (Tsuyoshi Sawada)
<p>My understanding is that the predicate method <code>Numeric#zero?</code> is not only a shorthand for <code>== 0</code>, but is also optimized for frequent patterns. If <code>zero?</code> is not faster than <code>== 0</code>, then it loses its reason for existence.</p>
<p>However, According to benchmarks on my environment, <code>number.zero?</code> is around 1.23 times to 1.64 times slower than <code>number == 0</code> when <code>number</code> is an <code>Integer</code>, <code>Rational</code>, or <code>Complex</code>. It is faster only when <code>number</code> is a <code>Float</code>.</p>
<p>And with <code>number.nonzero?</code>, it is even worse. It is about 1.88 times to 4.35 times slower than <code>number != 0</code>.</p>
<p>I think there is something wrong with this, and it should be possible to optimize these methods, which has somehow been missed.</p> Ruby master - Feature #15143 (Closed): Extend `Enumerable#to_h`https://bugs.ruby-lang.org/issues/151432018-09-20T02:23:27Zsawa (Tsuyoshi Sawada)
<p>Often, we call <code>Array#to_h</code> to the result of <code>Enumerable#map</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]}.</span><span class="nf">to_h</span>
<span class="c1">#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}</span>
</code></pre>
<p>I am thinking of a feature to do this in a single method call.</p>
<p>Currently, <code>Enumerable#to_h</code> does not accept a block. I propose that, when <code>Enumerable#to_h</code> is called with a block (that has a subarray representing a key-value pair), return a hash that would be returned by applying the block to <code>map</code>, and <code>to_h</code> to the result:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">to_h</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]}</span>
<span class="c1">#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}</span>
</code></pre>
<p>Ideally, I request this to be done internally to Ruby without creating an intermediate parent array.</p> Ruby master - Feature #14559 (Closed): ENV.slicehttps://bugs.ruby-lang.org/issues/145592018-02-28T08:39:22Zsawa (Tsuyoshi Sawada)
<p>I would like to request <code>ENV.slice</code>, which should behave analogous to <code>Hash#slice</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">ENV</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="s2">"PATH"</span><span class="p">,</span> <span class="s2">"FOO"</span><span class="p">)</span>
<span class="c1"># => {"PATH" => "/foo:/bar", "FOO" => "foo"}</span>
</code></pre> Ruby master - Feature #12079 (Rejected): Loosening the condition for refinementhttps://bugs.ruby-lang.org/issues/120792016-02-16T13:29:52Zsawa (Tsuyoshi Sawada)
<p>There are a few non-standard ways of calling a method that cannot be used when the relevant method is a refined method:</p>
<ul>
<li>a symbol used with <code>&</code> as a block via symbol to proc</li>
<li>a symbol used with <code>send</code> or <code>__send__</code>
</li>
</ul>
<p>For example, the following will fail:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"> <span class="k">module</span> <span class="nn">Foo</span>
<span class="n">refine</span> <span class="no">String</span>
<span class="k">def</span> <span class="nf">baz</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">using</span> <span class="no">Foo</span>
<span class="p">[</span><span class="s2">"a"</span><span class="p">,</span> <span class="s2">"b"</span><span class="p">,</span> <span class="s2">"c"</span><span class="p">].</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:baz</span><span class="p">)</span> <span class="c1"># => undefined method error</span>
<span class="s2">"a"</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="ss">:baz</span><span class="p">)</span> <span class="c1"># => undefined method error</span>
</code></pre>
<p>I would like to propose to loosen the condition for refinement so that as long as the relevant construction (such as the use of <code>&</code> to provoke <code>Symbol#to_proc</code> or calling of <code>send</code> or <code>__send__</code>) is within the valid scope of refinement, allow the call to the relevant methods.</p> Ruby master - Bug #11991 (Closed): `Symbol#match` returns the match position, unlike `String#matc...https://bugs.ruby-lang.org/issues/119912016-01-14T22:20:45Zsawa (Tsuyoshi Sawada)
<p><code>String#match</code> and <code>Regexp#match</code> return a <code>MatchData</code> when match succeeds:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="s2">""</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="sr">//</span><span class="p">)</span> <span class="c1"># => #<MatchData ""></span>
<span class="sr">//</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="c1"># => #<MatchData ""></span>
<span class="sr">//</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="ss">:""</span><span class="p">)</span> <span class="c1"># => #<MatchData ""></span>
</code></pre>
<p>But <code>Symbol#match</code> returns the match position (like <code>String#=~</code>):</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="ss">:""</span><span class="p">.</span><span class="nf">match</span><span class="p">(</span><span class="sr">//</span><span class="p">)</span> <span class="c1"># => 0</span>
</code></pre>
<p>Thus, <code>Symbol#match</code> behaves differently from <code>String#match</code> and <code>Regexp#match</code>. This is the documented behavior, but it may be a bug (together with the documentation).</p>
<p>On the other hand, if it is not a bug, what is the rationale?</p> Ruby master - Bug #11878 (Rejected): Comparison of prepended moduleshttps://bugs.ruby-lang.org/issues/118782015-12-26T14:44:08Zsawa (Tsuyoshi Sawada)
<p>Including module <code>B</code> to class/module <code>A</code> gives the following results (as expected):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">A</span><span class="p">;</span> <span class="k">end</span>
<span class="k">module</span> <span class="nn">B</span><span class="p">;</span> <span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">include</span> <span class="no">B</span>
<span class="no">A</span> <span class="o"><</span> <span class="no">B</span> <span class="c1"># => true</span>
<span class="no">B</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => false</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">B</span> <span class="c1"># => -1</span>
</code></pre>
<p>And prepending module <code>C</code> to <code>A</code> gives the following results:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">C</span><span class="p">;</span> <span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">prepend</span> <span class="no">C</span>
<span class="no">A</span> <span class="o"><</span> <span class="no">C</span> <span class="c1"># => true</span>
<span class="no">C</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => nil</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">C</span> <span class="c1"># => -1</span>
</code></pre>
<p>It looks like including and prepending almost do not make difference with respect to module comparison, i.e., <code>A < B</code> and <code>A < C</code> are the same, and <code>A <=> B</code> and <code>A <=> C</code> are the same. However, then, the difference between <code>B < A</code> and <code>C < A</code> stands out unexplained. I suppose this is a bug. If <code>C < A</code> were to return <code>false</code>, then it would be at least consistent.</p>
<p>However, if that was what was intended, then at least to me, it is strange. In that case, I would like to make this a feature request. I would rather expect:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">A</span> <span class="o"><</span> <span class="no">C</span> <span class="c1"># => false</span>
<span class="no">C</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => true</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">C</span> <span class="c1"># => 1</span>
</code></pre> Ruby master - Bug #11860 (Closed): Double splat does not work on empty hash assigned via variablehttps://bugs.ruby-lang.org/issues/118602015-12-22T08:33:54Zsawa (Tsuyoshi Sawada)
<p>When an empty hash is given as a literal, the double splat operates on it, and leaves nothing, which is expected.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">String</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="p">[</span><span class="o">**</span><span class="p">{}]</span> <span class="c1"># => []</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">foo</span><span class="p">(</span><span class="o">**</span><span class="p">{})</span> <span class="c1"># => nil</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="o">**</span><span class="p">{})</span> <span class="c1"># => nil</span>
</code></pre>
<p>However, when an empty hash is given via variable, the double splat retains an empty hash in place.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{}</span>
<span class="p">[</span><span class="o">**</span><span class="n">h</span><span class="p">]</span> <span class="c1"># => [{}]</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">foo</span><span class="p">(</span><span class="o">**</span><span class="n">h</span><span class="p">)</span> <span class="c1"># => wrong number of arguments (given 1, expected 0)</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="o">**</span><span class="n">h</span><span class="p">)</span> <span class="c1"># => wrong number of arguments (given 1, expected 0)</span>
</code></pre> Ruby master - Feature #11818 (Closed): `Hash#compact`https://bugs.ruby-lang.org/issues/118182015-12-15T05:09:54Zsawa (Tsuyoshi Sawada)
<p>I request <code>Hash#compact</code> and <code>Hash#compact!</code> that remove the key-value pairs whose value is <code>nil</code>, as follows:</p>
<pre><code>h1 = {a:, 1, b: nil, c: 2}
h1.compact # => {a: 1, c: 2}
h1 # => {a: 1, b: nil, c: 2}
h2 = {a:, 1, b: nil, c: 2}
h2.compact! # => {a: 1, c: 2}
h2 # => {a: 1, c: 2}
h3 = {a:, 1, c: 2}
h3.compact! # => nil
h3 # => {a: 1, c: 2}
</code></pre>
<p>I believe people have frequent need to do this.</p> Ruby master - Feature #11813 (Rejected): Extend safe navigation operator for [] and []= with synt...https://bugs.ruby-lang.org/issues/118132015-12-13T08:09:31Zsawa (Tsuyoshi Sawada)
<p>Now we have the safe navigation operator <code>&.</code>. But this cannot be used with syntax sugar form of the methods <code>[]</code> and <code>[]=</code>, which are more frequent than their ordinary forms of method call. For example, when <code>a</code> can be either an array or <code>nil</code>, we can do:</p>
<pre><code>a &.[](3)
a &.[]= 2, :foo
</code></pre>
<p>but we cannot do:</p>
<pre><code>a &.[3]
a &.[2] = :foo
</code></pre>
<p>It would be nice if we can extend the use of <code>&.</code> to cover syntactic sugar as above.</p> Ruby master - Bug #11805 (Closed): Cannot run Rubyhttps://bugs.ruby-lang.org/issues/118052015-12-11T10:24:09Zsawa (Tsuyoshi Sawada)
<p>I installed the new Ruby, and tried to run Ruby, but it raises the following error.</p>
<p>(Since I installed Ruby 2.3.0preview1, it had a bug, and I had not been able to run Ruby, and now, after having waited for a month, I installed the new one, and there still is a bug. Disappointed a little bit.)</p>
<pre><code>/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `initialize': Permission denied @ rb_sysopen - /usr/local/lib/ruby/gems/2.3.0/specifications/did_you_mean-1.0.0.beta3.gemspec (Errno::EACCES)
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `open'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `data'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:203:in `valid?'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:748:in `select'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:748:in `gemspec_stubs_in'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:773:in `block in map_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `each'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `flat_map'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `map_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:762:in `installed_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:830:in `stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1035:in `find_by_path'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems.rb:188:in `try_activate'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:124:in `rescue in require'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:39:in `require'
from <internal:gem_prelude>:6:in `<internal:gem_prelude>'
</code></pre> Ruby master - Feature #11797 (Closed): `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/117972015-12-09T16:15:51Zsawa (Tsuyoshi Sawada)
<p>Sometimes, when working with <code>Enumerator#with_object</code>, I want to keep some additional temporary objects besides the one to return. A use case is as follows (I got this from this StackOverflow question: <a href="http://stackoverflow.com/questions/3418123" class="external">http://stackoverflow.com/questions/3418123</a>). Suppose I have an enumerator created from an array:</p>
<pre><code>e = ["a", "b", "c", "c", "a", "c"].to_enum
</code></pre>
<p>and want to get an array of its repeated elements in the order they are repeated (i.e., appears for the second time):</p>
<pre><code># => ["c", "a"]
</code></pre>
<p>I can do it using <code>Enumerator#with_object</code> like this:</p>
<pre><code>e.to_enum.with_object([{}, []]){|c, (h, a)| h[c] ? a.push(c) : h.store(c, true)}.last.uniq
</code></pre>
<p>Here, I am getting the array referred to as <code>a</code> in the block, but besides that, I am using a temporal hash <code>h</code>. I thought it would be nice if <code>Enumerator#with_object</code> accepts one or more objects, pass them individually as block arguments, and returns only the last argument so that I can do this:</p>
<pre><code>e.to_enum.with_object({}, []){|c, h, a| h[c] ? a.push(c) : h.store(c, true)}.uniq
</code></pre> Ruby master - Feature #11761 (Rejected): `Hash#default_set` and `Hash#default_proc_set`https://bugs.ruby-lang.org/issues/117612015-12-02T06:34:55Zsawa (Tsuyoshi Sawada)
<p>I often want to assign a default value or proc to an existing hash <code>h</code> and chain method after it. The way I do it is:</p>
<pre><code>Hash.new(default_value).merge(h)...
Hash.new{default_proc_value}.merge(h)...
</code></pre>
<p>It would be convenient if I can assign them to a hash directly, and <code>Hash#default=</code> and <code>Hash#default_proc=</code> are close to doing that, but they are actually not useful because the return value is not the receiver hash:</p>
<pre><code>h.default = default_value; h...
h.default_proc = proc {default_proc_value}; h...
</code></pre>
<p>I wish there were methods <code>Hash#default_set</code> and <code>Hash#default_proc_set</code> that return the receiver hash:</p>
<pre><code>h.default_set(default_value)...
h.default_proc_set{default_proc_value}...
</code></pre> Ruby master - Bug #11691 (Closed): Permission denied @ rb_sysopen did_you_mean-1.0.0.beta3.gemspechttps://bugs.ruby-lang.org/issues/116912015-11-15T16:47:07Zsawa (Tsuyoshi Sawada)
<p>When I ruby Ruby, I get this error:</p>
<pre><code>/usr/local/lib/ruby/2.3.0/rubygems/stub_specification.rb:80:in `initialize': Permission denied @ rb_sysopen - /usr/local/lib/ruby/gems/2.3.0/specifications/did_you_mean-1.0.0.beta3.gemspec (Errno::EACCES)
from /usr/local/lib/ruby/2.3.0/rubygems/stub_specification.rb:80:in `open'
from /usr/local/lib/ruby/2.3.0/rubygems/stub_specification.rb:80:in `data'
from /usr/local/lib/ruby/2.3.0/rubygems/stub_specification.rb:186:in `valid?'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:749:in `select'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:749:in `gemspec_stubs_in'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:755:in `block in map_stubs'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:755:in `each'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:755:in `flat_map'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:755:in `map_stubs'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:806:in `stubs'
from /usr/local/lib/ruby/2.3.0/rubygems/specification.rb:1009:in `find_by_path'
from /usr/local/lib/ruby/2.3.0/rubygems.rb:188:in `try_activate'
from /usr/local/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:126:in `rescue in require'
from /usr/local/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:39:in `require'
from <internal:gem_prelude>:4:in `<compiled>'
</code></pre> Ruby master - Feature #11568 (Closed): Misleading warning for duplicate keys in a hashhttps://bugs.ruby-lang.org/issues/115682015-10-05T20:41:10Zsawa (Tsuyoshi Sawada)
<p>When a hash literal has duplicate keys as follows,</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">{</span>
<span class="ss">a: </span><span class="s2">"foo"</span><span class="p">,</span>
<span class="ss">a: </span><span class="s2">"bar"</span><span class="p">,</span>
<span class="p">}</span>
</code></pre>
<p>the warning message goes as follows:</p>
<pre><code>warning: duplicated key at line 3 ignored: :a
</code></pre>
<p>This message gives the impression as if it is the value <code>"bar"</code> at line 3 that is ignored, even though in reality it is the value <code>"foo"</code> at line 2 that is ignored. It is misleading. I suggest the message to be changed.</p> Ruby master - Bug #11471 (Closed): min, min_by, max, max_by with optional parameter return a wron...https://bugs.ruby-lang.org/issues/114712015-08-20T17:51:06Zsawa (Tsuyoshi Sawada)
<p>This is reported in StackOverflow: <a href="http://stackoverflow.com/questions/32121749/why-20-13-14-min2-13-20" class="external">http://stackoverflow.com/questions/32121749/why-20-13-14-min2-13-20</a>. Sometimes <code>min</code>, <code>min_by</code>, <code>max</code>, <code>max_by</code> with an optional parameter return a wrong value.</p>
<pre><code>[20, 32, 32, 21, 30, 25, 29, 13, 14].min(2) # => [13, 20]
[20, 32, 32, 21, 30, 25, 29, 13, 14].min_by(2, &:itself) # => [13, 20]
[0, 0, 0, 0, 0, 0, 1, 3, 2].max(2) # => [3, 1]
[0, 0, 0, 0, 0, 0, 1, 3, 2].max_by(2, &:itself) # => [3, 1]
</code></pre> Ruby master - Feature #11309 (Open): Iterator over string matcheshttps://bugs.ruby-lang.org/issues/113092015-06-26T14:55:43Zsawa (Tsuyoshi Sawada)
<p>This was hinted from a problem in stackoverflow (<a href="http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511" class="external">http://stackoverflow.com/questions/31074050/build-list-of-strings-containing-substrings-separated-by-an-from-a-string/31075511#31075511</a>).</p>
<p>Suppose there is a string:</p>
<pre><code>s = "a_b_c_d_e"
</code></pre>
<p>To get an array of pre-matches that result from matching <code>s</code> with <code>"_"</code>, I can do this:</p>
<pre><code>a = []
s.scan("_"){a.push($`)}
a # => ["a", "a_b", "a_b_c", "a_b_c_d"]
</code></pre>
<p>But this looks too Perlish. I thought it would be nice if there is a method on <code>String</code> that creates an enumerator over matches so that I can do something like this:</p>
<pre><code>"a_b_c_d_e".some_method("_").with_object([]){|m, a| a.push(m.post_match)}
# => ["a", "a_b", "a_b_c", "a_b_c_d"]
</code></pre>
<p>where <code>m</code> is the last matchdata instance at that point. I believe such method would have wider application.</p> Ruby master - Feature #11308 (Closed): Optional `include_super=true` parameter for `*method_defin...https://bugs.ruby-lang.org/issues/113082015-06-26T04:47:24Zsawa (Tsuyoshi Sawada)
<p>I request the following methods in the <code>Module</code> class:</p>
<ul>
<li><code>method_defined?</code></li>
<li><code>private_method_defined?</code></li>
<li><code>protected_method_defined?</code></li>
<li><code>public_method_defined?</code></li>
</ul>
<p>to take an optional <code>include_super</code> parameter with the default value <code>true</code>, and work similar to the methods:</p>
<ul>
<li><code>instance_methods</code></li>
<li><code>private_instance_methods</code></li>
<li><code>protected_instance_methods</code></li>
<li>and <code>public_instance_methods</code>
</li>
</ul> Ruby master - Feature #10880 (Rejected): Subclassing Array from Enumeratorhttps://bugs.ruby-lang.org/issues/108802015-02-21T20:04:33Zsawa (Tsuyoshi Sawada)
<p>It often happens that I want to use a method from the <code>Enumerator</code> class on an array, and so I frequently use <code>Array#to_enum</code>. Since <code>Array</code> and <code>Enumerator</code> share the same parent <code>Enumerable</code>, they should be very close. I think it would be convenient if <code>Array</code> inherits from <code>Enumerator</code>. (Perhaps the same thing can be said for <code>Hash</code> too)</p> Ruby master - Bug #10845 (Closed): Subclassing Stringhttps://bugs.ruby-lang.org/issues/108452015-02-10T23:41:25Zsawa (Tsuyoshi Sawada)
<p>If I make a subclass of <code>String</code>, the method <code>*</code> returns an instance of that class.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">MyString</span> <span class="o"><</span> <span class="no">String</span>
<span class="k">end</span>
<span class="no">MyString</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">).</span><span class="nf">*</span><span class="p">(</span><span class="mi">2</span><span class="p">).</span><span class="nf">class</span> <span class="c1">#=> MyString</span>
</code></pre>
<p>This is different from other similar operations like <code>+</code> and <code>%</code>, which return a <code>String</code> instance.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">MyString</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">).</span><span class="nf">+</span><span class="p">(</span><span class="s2">"bar"</span><span class="p">).</span><span class="nf">class</span> <span class="c1">#=> String</span>
<span class="no">MyString</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"%{foo}"</span><span class="p">).</span><span class="nf">%</span><span class="p">(</span><span class="ss">foo: </span><span class="s2">"bar"</span><span class="p">).</span><span class="nf">class</span> <span class="c1">#=> String</span>
</code></pre>
<p>I don't see clear reason why <code>*</code> is to be different from <code>+</code> and <code>%</code>, and thought that perhaps either the behaviour with <code>*</code> is a bug, or the behaviour with <code>+</code> and <code>%</code> is a bug.</p>
<p>Or, is a reason why they are different?</p> Ruby master - Feature #10797 (Closed): `inherit` parameter for `..._defined?` methods in Modulehttps://bugs.ruby-lang.org/issues/107972015-01-28T22:27:49Zsawa (Tsuyoshi Sawada)
<p><code>Module#const_defined?</code> takes an optional argument <code>inherit</code> with default value <code>true</code> to control whether to consider inheritance. But the following methods do not:</p>
<ul>
<li><code>Module#method_defined?</code></li>
<li><code>Module#private_method_defined?</code></li>
<li><code>Module#protected_method_defined?</code></li>
<li><code>Module#public_method_defined?</code></li>
</ul>
<p>They only allow what would correspond to <code>inherit = true</code>. I request an optional argument <code>inherit</code> with default value <code>true</code> to be added to these methods to control whether inheritance should be considered.</p> Ruby master - Feature #10771 (Closed): An easy way to get the source location of a constanthttps://bugs.ruby-lang.org/issues/107712015-01-22T05:50:18Zsawa (Tsuyoshi Sawada)
<p>For constants, it is difficult to get the source location where it was (last) defined. I request either of the following to be implemented:</p>
<ul>
<li>
<p>Tracepoint emits a signal when a constant is defined.</p>
</li>
<li>
<p>Implement a <code>Constant</code> class (similar to <code>Method</code> class) and a <code>constant</code> method (similar to <code>method</code> method) that behave as follows:</p>
<pre><code> foo1.rb
1| module Foo
2| Bar = :bar
3| end
4| Foo.constant(:Bar) #=> #<Constant: Foo#Bar>
5| Foo.constant(:Bar).source_location #=> ["foo1.rb", 2]
</code></pre>
</li>
<li>
<p>Implement <code>Module#constant_source_location</code></p>
<pre><code> foo2.rb
1| module Foo
2| Bar = :bar
3| end
4| Foo.constant_source_location(:Bar) #=> ["foo2.rb", 2]
</code></pre>
</li>
</ul> Ruby master - Feature #10769 (Closed): Negative counterpart to Enumerable#slice_whenhttps://bugs.ruby-lang.org/issues/107692015-01-22T00:35:57Zsawa (Tsuyoshi Sawada)
<p>It seems to me that most useful cases of <code>Enumerable#slice_when</code> involve a negative condition inside the block. That observation seems to be confirmed by the official examples in <a href="http://docs.ruby-lang.org/ja/2.2.0/method/Enumerable/i/slice_when.html" class="external">http://docs.ruby-lang.org/ja/2.2.0/method/Enumerable/i/slice_when.html</a>. In these examples, the conditions inside the block are negations of what would is intended (which is expressed in the comment above each code).</p>
<pre><code># 1ずつ増加する部分配列ごとに分ける。
[1,2,4,9,10,11,12,15,16,19,20,21]
.slice_when{|i, j| i + 1 != j}.to_a # => [[1, 2], [4], [9, 10, 11, 12], [15, 16], [19, 20, 21]]
# ソート済の配列を近い値(差が6以内)の部分配列ごとに分ける。
[3, 11, 14, 25, 28, 29, 29, 41, 55, 57]
.slice_when{|i, j| 6 < j - i}.to_a # => [[3], [11, 14], [25, 28, 29, 29], [41], [55, 57]]
# 増加のみの部分配列ごとに分ける。
[0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
.slice_when{|i, j| i > j}.to_a # => [[0, 9], [2, 2, 3], [2, 7], [5, 9], [5]]
# 隣り合う偶数同士、奇数同士の部分配列ごとに分ける。
[7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
.slice_when{|i, j| i.even? != j.even?}.to_a # => [[7, 5, 9], [2, 0], [7, 9], [4, 2, 0]]
</code></pre>
<p>I propose that there should be a method on <code>Enumerable</code> that works like <code>slice_when</code> except that it works with the block negatively as compared to <code>slice_when</code>. Let us call this method <code>Enumerable#chunk_while</code>. Then, the examples above would be written more naturally without having a negative notion in the blocks.</p>
<pre><code># 1ずつ増加する部分配列ごとに分ける。
[1,2,4,9,10,11,12,15,16,19,20,21]
.chunk_while{|i, j| i + 1 == j}.to_a
# ソート済の配列を近い値(差が6以内)の部分配列ごとに分ける。
[3, 11, 14, 25, 28, 29, 29, 41, 55, 57]
.chunk_while{|i, j| j - i <= 6}.to_a
# 増加のみの部分配列ごとに分ける。
[0, 9, 2, 2, 3, 2, 7, 5, 9, 5]
.chunk_while{|i, j| i <= j}.to_a
# 隣り合う偶数同士、奇数同士の部分配列ごとに分ける。
[7, 5, 9, 2, 0, 7, 9, 4, 2, 0]
.chunk_while{|i, j| i.even? == j.even?}.to_a
</code></pre>
<p>I am not sure about the method name. There can be a better name.</p> Ruby master - Feature #10729 (Open): Array method to subtract in placehttps://bugs.ruby-lang.org/issues/107292015-01-10T22:36:02Zsawa (Tsuyoshi Sawada)
<p>I request a method on array that takes another array, subtract that from self in place (= destructively), and return the subtracted elements:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method([2, 4, 6]) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>Ideally, it should also allow a block:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method(&:even?) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>This operation is quite frequent, but at the moment, it requires several steps to do.</p> Ruby master - Feature #10482 (Feedback): Allow ignored items to vary in `Enumerable#chunk`.https://bugs.ruby-lang.org/issues/104822014-11-06T07:56:37Zsawa (Tsuyoshi Sawada)
<p>In <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Combined map/select method (Closed)" href="https://bugs.ruby-lang.org/issues/5663">#5663</a>, regarding a method proposed, Yehuda Katz's writes:</p>
<pre><code>The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome.
</code></pre>
<p>I would like to note here that the same problem exists with <code>Enumerable#chunk</code>. Currently, when the key value is <code>nil</code>, the corresponding items are thrown out. That may be useful sometimes, but sometimes, silently doing so causes a hard-to-detect bug. At least, there should be a way to change what is ignored (which would not break existing code using it), and ideally, nothing should be thrown out unless explicitly specified (which would break existing code).</p>
<p>I propose <code>Enumerable#chunk</code> to take an optional named parameter <code>ignore</code>, which switches what is ignored. When something other than <code>nil</code> is specified, then <code>nil</code> should not be ignored:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="ss">:foo1</span><span class="p">,</span> <span class="ss">:foo2</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">chunk</span><span class="p">(</span><span class="ss">ignore: </span><span class="no">String</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="p">.</span><span class="nf">class</span><span class="p">}</span>
<span class="c1"># => [[Symbol, [:foo1, :foo2]], [NilClass, [nil, nil]]]</span>
</code></pre>
<p>When you don't want anything to be ignored, then the parameter should be set to something that does not appear in the receiver:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="ss">:foo1</span><span class="p">,</span> <span class="ss">:foo2</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">chunk</span><span class="p">(</span><span class="ss">ignore: </span><span class="s2">"nothing to ignore"</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="p">.</span><span class="nf">class</span><span class="p">}</span>
<span class="c1"># => [[Symbol, [:foo1, :foo2]], [String, ["bar"]], [NilClass, [nil, nil]]]</span>
</code></pre> Ruby master - Feature #10463 (Rejected): :~@ and :!@ are not parsed correctlyhttps://bugs.ruby-lang.org/issues/104632014-11-01T02:13:11Zsawa (Tsuyoshi Sawada)
<p>The at mark in literal symbols <code>:~@</code> and <code>:!@</code> are ignored.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="ss">:~</span><span class="err">@</span> <span class="c1"># => :~</span>
<span class="p">:</span><span class="o">!</span><span class="err">@</span> <span class="c1"># => :!</span>
</code></pre> Ruby master - Feature #10426 (Open): A predicate to express congruencehttps://bugs.ruby-lang.org/issues/104262014-10-26T00:55:20Zsawa (Tsuyoshi Sawada)
<p>I occasionally felt the necessity of a predicate that checks congruence with respect to some operations:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Object</span>
<span class="k">def</span> <span class="nf">equal_by?</span> <span class="n">other</span><span class="p">,</span> <span class="o">&</span><span class="n">pr</span>
<span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span> <span class="o">==</span> <span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">alias</span> <span class="n">congruent?</span> <span class="n">equal_by?</span>
<span class="k">end</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">congruent?</span><span class="p">(</span><span class="mi">2</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">3</span><span class="p">}</span> <span class="c1">#=> true</span>
<span class="s2">"HELLO"</span><span class="p">.</span><span class="nf">equal_by?</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">,</span> <span class="o">&</span><span class="ss">:downcase</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre> Ruby master - Feature #10425 (Open): A predicate method to tell if a number is near anotherhttps://bugs.ruby-lang.org/issues/104252014-10-26T00:38:58Zsawa (Tsuyoshi Sawada)
<p>A method <code>near?</code> like the following would be useful.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Numeric</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span> <span class="o">-</span> <span class="n">to_f</span><span class="p">).</span><span class="nf">abs</span> <span class="o"><=</span> <span class="n">delta</span><span class="p">.</span><span class="nf">to_f</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Time</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="n">to_f</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span><span class="p">,</span> <span class="ss">delta: </span><span class="n">delta</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>It can be used to check errors, or whether something is around something.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">23.24324</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mf">23.23</span><span class="p">,</span> <span class="ss">delta: </span><span class="mf">0.5</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">t2</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t3</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span> <span class="c1">#=> true</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t3</span><span class="p">)</span> <span class="c1">#=> false</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="ss">delta: </span><span class="mi">1</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre>
<p>Some testing frameworks have something similar to this, but I think this is an elementary concept that Ruby should support at it core.</p> Ruby master - Bug #10422 (Closed): RDoc for BasicObject has methods that do not existhttps://bugs.ruby-lang.org/issues/104222014-10-23T07:44:37Zsawa (Tsuyoshi Sawada)
<p>RDoc for BasicObject (<a href="http://www.ruby-doc.org/core-2.1.3/BasicObject.html" class="external">http://www.ruby-doc.org/core-2.1.3/BasicObject.html</a>) has descriptions for methods <code>object_id</code> and <code>send</code>, which do not exist. Also, <code>send</code> and <code>__send__</code> are listed twice.</p> Ruby master - Feature #10406 (Rejected): Method starting with a capital is not detected when pare...https://bugs.ruby-lang.org/issues/104062014-10-20T11:32:52Zsawa (Tsuyoshi Sawada)
<p>A method whose name starts with a capital is not detected when parentheses and receiver are omitted.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">Foo</span><span class="p">;</span> <span class="nb">puts</span> <span class="s2">"foo"</span> <span class="k">end</span>
<span class="no">Foo</span> <span class="c1"># => uninitialized constant Foo</span>
</code></pre>
<p>If I disambiguate it as a method, then the method is detected:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Foo</span><span class="p">()</span> <span class="c1"># => foo</span>
</code></pre>
<p>This kind of consideration if unnecessary if the method name does not start with a capital.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">foo</span><span class="p">;</span> <span class="nb">puts</span> <span class="s2">"foo"</span> <span class="k">end</span>
<span class="n">foo</span> <span class="c1"># => foo</span>
</code></pre>
<p>So I expect method <code>Foo</code> to be detected without explicit receiver and arguments (unless there is a constant with the same name).</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">Foo</span><span class="p">;</span> <span class="nb">puts</span> <span class="s2">"foo"</span> <span class="k">end</span>
</code></pre>
<p>I first thought this as a bug. If it is not a bug, then I would like to ask this as a feature request.</p> Ruby master - Feature #10394 (Feedback): An instance method on Enumerator that evaluates the bloc...https://bugs.ruby-lang.org/issues/103942014-10-16T14:23:15Zsawa (Tsuyoshi Sawada)
<p><strong>Background</strong></p>
<p>There has been desire to omit the <code>| |</code> and the explicit receiver in a block used with an enumerator or an enumerable. Currently, when the content of the block is a single method that takes no argument, symbol-to-proc is used with the <code>&</code> syntax so that:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>can be written as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:upcase</span><span class="p">)</span>
</code></pre>
<p>There has repeated been proposals (<a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: map/collect extension which handles arguments (Open)" href="https://bugs.ruby-lang.org/issues/8987">#8987</a>, <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: New one-argument block syntax: &. (Feedback)" href="https://bugs.ruby-lang.org/issues/9076">#9076</a>, <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: [PATCH 0/n] Let underscore be positionally matched arg to omit binding obvious variable. (Open)" href="https://bugs.ruby-lang.org/issues/10318">#10318</a>) that express this desire to do this even when the block involves a method chain or a method with arguments like the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">concat</span><span class="p">(</span><span class="s2">"ber"</span><span class="p">)}</span>
<span class="p">[</span><span class="s2">" foo "</span><span class="p">,</span> <span class="s2">"</span><span class="se">\t</span><span class="s2">bar</span><span class="se">\n</span><span class="s2">"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">strip</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>Focus has been on modifying how a block is passed to the enumerable/enumerator, and there has not been consensus on how the syntax should be.</p>
<p><strong>Proposal</strong></p>
<p>Unlike the earlier proposals, I suggest that there should be an instance method on <code>Enumerator</code>, let's say <code>Enumerator#as_self</code>, that evaluates the block each time with <code>self</code> being the block variable that would be passed otherwise. With such method, the cases above would be written like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">.</span><span class="nf">as_self</span><span class="p">{</span><span class="n">concat</span><span class="p">(</span><span class="s2">"ber"</span><span class="p">)}</span>
<span class="p">[</span><span class="s2">" foo "</span><span class="p">,</span> <span class="s2">"</span><span class="se">\t</span><span class="s2">bar</span><span class="se">\n</span><span class="s2">"</span><span class="p">].</span><span class="nf">map</span><span class="p">.</span><span class="nf">as_self</span><span class="p">{</span><span class="n">strip</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>This adds no modification to the syntax, it just requires a new method <code>Enumerator#as_self</code> to be implemented. I consider this method being along the lines of <code>Enumerator#with_index</code>, <code>Enumerator#with_object</code>; it intervenes between an enumerator (related to a block-taking method) and a block, and let the block-taking method work in a modified way.</p>
<p>It resembles <code>instance_eval</code>, but is different in that it assigns to <code>self</code> what would be a block variable (which changes for each iteration), instead of assigning the receiver.</p> Ruby master - Feature #10366 (Open): New inspection form for rationalhttps://bugs.ruby-lang.org/issues/103662014-10-11T00:50:15Zsawa (Tsuyoshi Sawada)
<p>Currently, rational inspection is expressed with parentheses:</p>
<pre><code>(2/51)
</code></pre>
<p>If this were taken as a Ruby expression, it would mean integer division, whose value is <code>0</code> in this case. It does not make much sense to express that it is indeed not integer division and that it is rational by using parentheses. Now that we have rational literal using <code>r</code>, we can make the inspection form as:</p>
<pre><code>2/51r
</code></pre>
<p>This would be much less confusing.</p> Ruby master - Feature #10365 (Rejected): `Rational#to_a` and `Array#to_r`https://bugs.ruby-lang.org/issues/103652014-10-11T00:37:11Zsawa (Tsuyoshi Sawada)
<p>I guess people would want to extract the numerator and the denominator of an array. <code>to_a</code> would perhaps be a good name:</p>
<pre><code>(2/51).to_a # => [2, 51]
</code></pre>
<p>It also might be useful if <code>Array#to_r</code> were provided as an alternative way to create a rational from an array:</p>
<pre><code>[4, 6].to_r #=> (2/3)
</code></pre>
<p>This would be equivalent to <code>Rational(*[4, 6])</code>.</p> Ruby master - Feature #10343 (Rejected): Postfix notations for `when` and `else` inside `case` st...https://bugs.ruby-lang.org/issues/103432014-10-08T16:42:45Zsawa (Tsuyoshi Sawada)
<p>In a <code>case</code> statement, the condition part (<code>when ...</code>) in the branches do not have the same length in general, and especially, <code>else</code> is much shorter than the conditions. So when we write the condition and the return value in a single line, they are not aligned, and are hard to read.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="n">foo</span>
<span class="k">when</span> <span class="n">some_very_long_proc</span> <span class="k">then</span> <span class="s2">"a"</span>
<span class="k">when</span> <span class="n">short_regex</span> <span class="k">then</span> <span class="s2">"bbb"</span>
<span class="k">when</span> <span class="n">some_long_regex</span> <span class="k">then</span> <span class="s2">"cc"</span>
<span class="k">else</span> <span class="s2">"dddd"</span>
<span class="k">end</span>
</code></pre>
<p>I propose to allow postfix notations with <code>when</code> and <code>else</code> (or <code>otherwise</code>) inside <code>case</code> statement as below:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="n">foo</span>
<span class="s2">"a"</span> <span class="k">when</span> <span class="n">some_very_long_proc</span>
<span class="s2">"bbb"</span> <span class="k">when</span> <span class="n">short_regex</span>
<span class="s2">"cc"</span> <span class="k">when</span> <span class="n">some_long_regex</span>
<span class="s2">"dddd"</span> <span class="k">else</span>
<span class="k">end</span>
</code></pre>
<p>Pros are:</p>
<ol>
<li>
<p>Postfix notation does not require <code>then</code> or <code>;</code>, so it is concise.</p>
</li>
<li>
<p>The return values from the branches (e.g., <code>"a"</code>, <code>"bb"</code>, <code>"ccc"</code>, <code>"dddd"</code>) tend to be shorter and more uniformly lengthened than the conditions, hence they are somewhat close to being aligned naturally, making this easier to read.</p>
</li>
<li>
<p>We are usually more interested in the return value than the condition of a branch, especially when we are reading someone's code and are trying to grasp what the <code>case</code> statement does or returns.</p>
</li>
<li>
<p>This notation is closer to case-like conditional notations regularly used in mathematics:</p>
<pre><code> ┌ 1 (x = 0)
x! = ┤
└ x (x - 1)! (otherwise)
</code></pre>
<p>So it would be easier to read for those who are familiar with mathematics.</p>
</li>
</ol> Ruby master - Feature #10332 (Open): Rational literal for mixed fractionshttps://bugs.ruby-lang.org/issues/103322014-10-06T14:55:48Zsawa (Tsuyoshi Sawada)
<p>Current behavior of rational literal and <code>String#to_r</code> does not recognize mixed fractions. Mixed fraction is not rare, and is frequently used in places such as US length measurement.</p>
<p><img src="http://www.strongtie.com/graphics/anchorsystems/catalog/tables/217b-2012.gif" alt="drill bits"></p>
<p>I propose that rational literal and <code>String#to_r</code> should be extended to handle mixed fractions. Perhaps something like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">8</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="n">r</span> <span class="c1">#=> (17/2)</span>
<span class="s2">"8 1/2"</span><span class="p">.</span><span class="nf">to_r</span> <span class="c1">#=> (17/2)</span>
</code></pre>
<p>or</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">8</span> <span class="mi">1</span><span class="o">/</span><span class="mi">2</span><span class="p">)</span><span class="n">r</span> <span class="c1">#=> (17/2)</span>
</code></pre> Ruby master - Feature #10331 (Open): String#to_r to recognize negative denominatorshttps://bugs.ruby-lang.org/issues/103312014-10-06T14:44:18Zsawa (Tsuyoshi Sawada)
<p>Current behavior or <code>String#to_r</code> does not recognize negative denominators. This can lead to confusing results:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"1/-3"</span><span class="p">.</span><span class="nf">to_r</span>
<span class="c1">#=> (1/1)</span>
</code></pre>
<p>I propose negative denominators to be recognized.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"1/-3"</span><span class="p">.</span><span class="nf">to_r</span>
<span class="c1">#=> (-1/3)</span>
</code></pre> Ruby master - Bug #10260 (Third Party's Issue): Cannot install "thin" gemhttps://bugs.ruby-lang.org/issues/102602014-09-18T18:39:19Zsawa (Tsuyoshi Sawada)
<p>When I try to install "thin" gem, I get the following error:</p>
<pre><code>$ sudo gem install thin
Building native extensions. This could take a while...
ERROR: Error installing thin:
ERROR: Failed to build gem native extension.
/usr/local/bin/ruby -r ./siteconf20140919-24244-x0j5an.rb extconf.rb
checking for rb_trap_immediate in ruby.h,rubysig.h... no
checking for rb_thread_blocking_region()... no
checking for inotify_init() in sys/inotify.h... yes
checking for writev() in sys/uio.h... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_enable_interrupt()... no
checking for rb_time_new()... yes
checking for sys/event.h... no
checking for epoll_create() in sys/epoll.h... yes
creating Makefile
make "DESTDIR=" clean
make "DESTDIR="
compiling binder.cpp
compiling kb.cpp
compiling ssl.cpp
compiling rubymain.cpp
compiling ed.cpp
compiling cmain.cpp
compiling pipe.cpp
compiling em.cpp
em.cpp: In member function ‘void EventMachine_t::_RunEpollOnce()’:
em.cpp:574:37: error: ‘rb_thread_select’ was not declared in this scope
EmSelect (0, NULL, NULL, NULL, &tv);
^
em.cpp: In member function ‘int SelectData_t::_Select()’:
em.cpp:827:67: error: ‘rb_thread_select’ was not declared in this scope
return EmSelect (maxsocket+1, &fdreads, &fdwrites, &fderrors, &tv);
^
em.cpp: In member function ‘void EventMachine_t::_RunSelectOnce()’:
em.cpp:946:40: error: ‘rb_thread_select’ was not declared in this scope
EmSelect (0, NULL, NULL, NULL, &tv);
^
make: *** [em.o] Error 1
make failed, exit code 2
Gem files will remain installed in /usr/local/lib/ruby/gems/2.2.0/gems/eventmachine-1.0.3 for inspection.
Results logged to /usr/local/lib/ruby/gems/2.2.0/extensions/x86_64-linux/2.2.0-static/eventmachine-1.0.3/gem_make.out
</code></pre>
<p>I didn't have this problem with previous version of Ruby.</p> Ruby master - Feature #10254 (Feedback): Array#each and Array#map for nested arrayshttps://bugs.ruby-lang.org/issues/102542014-09-18T02:33:21Zsawa (Tsuyoshi Sawada)
<p>In order to allow iteration over elements of arrays nested within an array, I propose to pass <code>Array#each</code> and <code>Array#map</code> an optional argument that expresses the depth to iterate over.</p>
<p>Conventionally, iterating over nested elements requires nested <code>each</code> or <code>map</code>:</p>
<pre><code>[[1, 2], [3, 4], [5, 6]].map{|a| a.map{|e| e + 1}} #=> [[2, 3], [4, 5], [6, 7]]
[[[1, 2], [3, 4]], [[5, 6]]].map{|a| a.map{|a| a.map{|e| e + 1}}} #=> [[[2, 3], [4, 5]], [[6, 7]]]
</code></pre>
<p>With the proposed optional argument, this would be done by:</p>
<pre><code>[[1, 2], [3, 4], [5, 6]].map(1){|e| e + 1} #=> [[2, 3], [4, 5], [6, 7]]
[[[1, 2], [3, 4]], [[5, 6]]].map(2){|e| e + 1} #=> [[[2, 3], [4, 5]], [[6, 7]]]
</code></pre>
<p>Absence of the parameter should be understood as the parameter being defaulted to <code>0</code>.</p>
<pre><code>[1, 2, 3, 4, 5, 6].map{|e| e + 1} #=> [2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6].map(0){|e| e + 1} #=> [2, 3, 4, 5, 6, 7]
</code></pre> Ruby master - Feature #10240 (Rejected): `String#to_a`https://bugs.ruby-lang.org/issues/102402014-09-14T14:51:38Zsawa (Tsuyoshi Sawada)
<p>I often want to exclude the empty strings from an array of strings. A use case is to join an array of strings with <code>", "</code>, removing the empty ones, as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"Foo"</span><span class="p">,</span> <span class="s2">""</span><span class="p">,</span> <span class="s2">"Bar"</span><span class="p">].</span><span class="nf">reject</span><span class="p">(</span><span class="o">&</span><span class="ss">:empty?</span><span class="p">).</span><span class="nf">join</span><span class="p">(</span><span class="s2">", "</span><span class="p">)</span> <span class="c1"># => "Foo, Bar"</span>
</code></pre>
<p>In a similar situation with arrays of arrays, the empty ones can be excluded by using <code>*</code> and <code>Array#to_a</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="o">*</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="o">*</span><span class="p">[],</span> <span class="o">*</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">]]</span> <span class="c1"># => [1, 2, 3, 4]</span>
</code></pre>
<p>I would like to propose <code>String#to_a</code> defined as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">String</span>
<span class="k">def</span> <span class="nf">to_a</span><span class="p">;</span> <span class="n">empty?</span> <span class="p">?</span> <span class="p">[]</span> <span class="p">:</span> <span class="p">[</span><span class="nb">self</span><span class="p">]</span> <span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>Then we can do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="o">*</span><span class="s2">"Foo"</span><span class="p">,</span> <span class="o">*</span><span class="s2">""</span><span class="p">,</span> <span class="o">*</span><span class="s2">"Bar"</span><span class="p">].</span><span class="nf">join</span><span class="p">(</span><span class="s2">", "</span><span class="p">)</span>
</code></pre> Ruby master - Bug #10184 (Closed): irb terminates with no method errorhttps://bugs.ruby-lang.org/issues/101842014-08-29T16:33:54Zsawa (Tsuyoshi Sawada)
<p>When I input the following lines in irb, the irb terminates.</p>
<pre><code>$ irb
irb(main):001:0> def !; end
=> :!
irb(main):002:0> !
/usr/local/lib/ruby/2.1.0/irb/input-method.rb:153:in `gets': private method `!' called for false:FalseClass (NoMethodError)
...
$
</code></pre>
<p>A corresponding question is posted on StackOverflow at <a href="http://stackoverflow.com/questions/25572126/irb-terminates-with-no-method-error/25572313#25572313" class="external">http://stackoverflow.com/questions/25572126/irb-terminates-with-no-method-error/25572313#25572313</a>.</p> Ruby master - Feature #10183 (Open): An alternative name for method `class`https://bugs.ruby-lang.org/issues/101832014-08-29T12:52:13Zsawa (Tsuyoshi Sawada)
<p>The method <code>class</code> is special in that it always has to have an explicit receiver in order to avoid crash with the keyword <code>class</code>. But this is very inconvenient. I have seen so many</p>
<pre><code>self.class
</code></pre>
<p>in codes. I propose that there should be an alternative name for this method so that it can be used with an implicit receiver, and the method name <code>class</code> should be gradually depricated.</p>
<p>As for the name, I have no clear idea. I can only think of <code>klass</code>, but perhaps someone else might come up with a better name.</p> Ruby master - Feature #10131 (Feedback): Greatest multiple of r not greater than xhttps://bugs.ruby-lang.org/issues/101312014-08-14T06:32:16Zsawa (Tsuyoshi Sawada)
<p>It happens too often to me that I want to calculate the greatest multiple of numeric r not greater than numeric x. This can be done by a simple operation below:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="mf">3.7</span><span class="p">,</span> <span class="mf">0.5</span>
<span class="n">x</span><span class="p">.</span><span class="nf">div</span><span class="p">(</span><span class="n">r</span><span class="p">).</span><span class="nf">*</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="c1">#=> 3.5</span>
<span class="n">x</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">41</span><span class="p">,</span> <span class="mi">6</span>
<span class="n">x</span><span class="p">.</span><span class="nf">div</span><span class="p">(</span><span class="n">r</span><span class="p">).</span><span class="nf">*</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="c1">#=> 36</span>
</code></pre>
<p>but I encounter this operation so often that I think it is an essential operation, and should be done as a single operation. Can any of the existing zero-ary operation be extended to take an optional argument to do this calculation? For example</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">3.7</span><span class="p">.</span><span class="nf">floor</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span> <span class="c1">#=> 3.5</span>
<span class="mi">41</span><span class="p">.</span><span class="nf">floor</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="c1">#=> 36</span>
</code></pre> Ruby master - Feature #10130 (Rejected): String format with nested hashhttps://bugs.ruby-lang.org/issues/101302014-08-14T00:10:31Zsawa (Tsuyoshi Sawada)
<p>When we do string format with hashes, we have the restriction that the hash cannot be a nested one. When we want to handle more complicated string formats, for example in templates, it is more convenient if we can use a nested hash.</p>
<pre><code>"Author: %{author.name} (%{author.affiliation}), %{date}" % {author: {name: "Ruby Taro", affiliation: "Ruby co."}, date: "2014, 8, 14"}
#=> "Author: Ruby Taro (Ruby co.), 2014, 8, 14"
</code></pre> Ruby master - Feature #10118 (Closed): Double splat for non-symbol keyshttps://bugs.ruby-lang.org/issues/101182014-08-07T23:45:13Zsawa (Tsuyoshi Sawada)
<p>The double splat operator ** only seems to work with hashes whose keys are symbols. It will not work when a key is a string, for example. This is true for both ways; for construction:</p>
<pre><code>def foo **; end
foo(:a => 3) #=> nil
foo("a" => 3) #=> ArgumentError: wrong number of arguments (1 for 0)
</code></pre>
<p>and destruction:</p>
<pre><code>def bar *; end
bar(**{:a => 3}) #=> nil
bar(**{"a" => 3}) #=> TypeError: wrong argument type String (expected Symbol)
</code></pre>
<p>This is confusing. I propose that the double splat syntax should be extended so that it works even when the keys are not symbols.</p> Ruby master - Bug #10049 (Closed): RDoc bug for time formathttps://bugs.ruby-lang.org/issues/100492014-07-16T20:26:06Zsawa (Tsuyoshi Sawada)
<p>A bug report <a href="https://bugs.ruby-lang.org/issues/8941" class="external">https://bugs.ruby-lang.org/issues/8941</a> notes a contradiction between RDoc and Ruby behavior. If tadayoshi funaba is correct and <code>%Y</code> should be able to accept digits less than four, then the following RDoc description is a bug, and should be corrected.</p>
<pre><code>%Y - Year with century (can be negative, 4 digits at least)
</code></pre> Ruby master - Feature #9948 (Closed): Safely insert an object into an arrayhttps://bugs.ruby-lang.org/issues/99482014-06-17T00:40:48Zsawa (Tsuyoshi Sawada)
<p>When I insert an object using <code>Array#insert(index, object)</code> method (or <code>Array#[index]= object</code>), it happened sometimes that I mistakenly passed <code>index</code> that is too big, and too often in such cases was the bug very difficult to detect because in such case, <code>insert</code> will silently insert <code>nil</code> to fill up the empty slots:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="ss">:foo</span><span class="p">,</span> <span class="ss">:bar</span><span class="p">].</span><span class="nf">insert</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="ss">:baz</span><span class="p">)</span> <span class="c1"># => [:foo, :bar, nil, nil, :baz]</span>
</code></pre>
<p>On the other hand, when the index is too small (negative), <code>index</code> raises an index error. I wish that an error were raised when the index is too big (when <code>index</code> is greater than <code>length</code> of <code>self</code>), but it is perhaps a bad idea to change the feature of <code>insert</code> by now. So I propose that a new method being added, something like <code>Array#safely_insert</code>, which works the same as <code>insert</code>, except that when <code>index</code> is larger than the size of the array, it should raise:</p>
<pre><code>Index Error: index #{indexs} too large for array; maximum: #{length}
</code></pre> Ruby master - Bug #9900 (Closed): Segmentation fault with recursive reference of a hashhttps://bugs.ruby-lang.org/issues/99002014-06-04T04:10:47Zsawa (Tsuyoshi Sawada)
<p>The following code raises a segmentation fault.</p>
<pre><code>Hash.new{|h,k| h[k]}[:foo]
# => [BUG] Segmentation fault at 0x007fff5f3fff80
</code></pre>
<p>This bug was found on stackoverflow <a href="http://stackoverflow.com/questions/24028557/irb-crash-segmentation-bug-and-ruby-syntax-clarification" class="external">http://stackoverflow.com/questions/24028557/irb-crash-segmentation-bug-and-ruby-syntax-clarification</a></p> Ruby master - Bug #9883 (Closed): Different behaviour between `==` and hash key lookup regarding ...https://bugs.ruby-lang.org/issues/98832014-05-30T04:06:14Zsawa (Tsuyoshi Sawada)
<p>This question is reported in stackoverflow (<a href="http://stackoverflow.com/questions/23946127" class="external">http://stackoverflow.com/questions/23946127</a>). When a multibyte character is split by <code>slice</code>, <code>==</code> ignores the split fragment of the character, while key lookup on hash does not:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">s1</span> <span class="o">=</span> <span class="s2">"’xxxxxxxxxxxxxxxxxxxxxxxx"</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">24</span><span class="p">)</span>
<span class="n">s2</span> <span class="o">=</span> <span class="s2">"xxxxxxxxxxxxxxxxxxxxxxxx"</span>
<span class="n">s1</span> <span class="o">==</span> <span class="n">s2</span> <span class="c1"># => true</span>
<span class="p">{</span><span class="n">s2</span><span class="o">=></span><span class="s2">""</span><span class="p">}.</span><span class="nf">key?</span><span class="p">(</span><span class="n">s1</span><span class="p">)</span> <span class="c1"># => false</span>
</code></pre>
<p>This is inconsistent, and I believe either of the above two is a bug. If it is not a bug, but intended, then it is misleading, and I propose the feature should be changed.</p> Ruby master - Bug #9873 (Closed): YAML conversion of empty symbolhttps://bugs.ruby-lang.org/issues/98732014-05-28T15:26:59Zsawa (Tsuyoshi Sawada)
<p>The yaml gem changes the empty symbol into a string of a colon:</p>
<pre><code>require "yaml"
:"".to_yaml # => "--- ! ':'\n"
</code></pre>
<p>So, the following round trip fails to maintain the original:</p>
<pre><code>YAML.load(:"".to_yaml) # => ":"
</code></pre> Ruby master - Feature #9805 (Closed): Backtrace for SystemStackErrorhttps://bugs.ruby-lang.org/issues/98052014-05-05T20:11:50Zsawa (Tsuyoshi Sawada)
<p>The following code:</p>
<pre><code>def a; a end
begin
a
rescue => e
puts e.backtrace.inspect
end
</code></pre>
<p>only returns a single line of backtrace like:</p>
<pre><code>/tmp/scratch:3:in `a'
</code></pre>
<p>but this is not helpful when debugging. It would be better if some limit be set by configuration, and the backtrace is displayed as far as possible within the limit, so the case above would become:</p>
<pre><code>/tmp/scratch:3:in `a'
/tmp/scratch:3:in `a'
/tmp/scratch:3:in `a'
... # repeated as many times as the limit set
/tmp/scratch:3:in `a'
</code></pre>
<p>I believe the same question was asked here: <a href="https://bugs.ruby-lang.org/issues/6216" class="external">https://bugs.ruby-lang.org/issues/6216</a>, but the code posted by the poster was unnecessarily long and complex that the intent was not clear to many people.</p> Ruby master - Feature #9768 (Assigned): Method that is visible only within a certain module/classhttps://bugs.ruby-lang.org/issues/97682014-04-22T09:57:35Zsawa (Tsuyoshi Sawada)
<p>Some frameworks/libraries monkeypatch their own methods on Ruby core classes like <code>String</code>, <code>Hash</code>, <code>Array</code>, etc., and that is often causing problems/concerns of name conflict.</p>
<p>Seeing that these custom methods are used only in the context of a certain module/class, I request for a way to define a method (<code>foo</code>) on a module/class (<code>A</code>) so that it will be visible only from within a specified module/class (<code>B</code>) or its descendants. The following illustrates this situation:</p>
<pre><code>A.new.foo # => undefined
class B
A.new.foo # => defined
def bar
A.new.foo # => defined
end
def self.baz
A.new.foo # => defined
end
end
class C < B
A.new.foo # => defined
def bar
A.new.foo # => defined
end
def self.baz
A.new.foo # => defined
end
end
</code></pre>
<p>I do not have a certain syntax for this in mind, but I think it can be made much simpler, compared to the complicated syntax of refinement.</p>
<p>This is reminiscent of refinement, but they are pretty much different.</p>
<p>Refinement's purpose is to let certain methods be accessible from only a certain file. A typical use case would be a library developer defining their methods to be used from within the library and making such methods inaccessible from the end user.</p>
<p>On the other hand, the idea I am proposing here is for making method accessible from any file, but only within a certain module/class. A typical use case would be defining a method to be used by an end user, but only from within a context of certain module/class.</p> Ruby master - Bug #9728 (Closed): Regexp bughttps://bugs.ruby-lang.org/issues/97282014-04-11T06:41:13Zsawa (Tsuyoshi Sawada)
<p>As reported in StackOverflow (<a href="http://stackoverflow.com/questions/23004527" class="external">http://stackoverflow.com/questions/23004527</a>) (with a wrong expectation), the following regex pattern with the <code>*</code> operator does not match.</p>
<pre><code>"ab" =~ /(?!^a).*b/
# => nil
</code></pre>
<p>When <code>?</code> is used instead, it seems to match correctly:</p>
<pre><code>"ab" =~ /(?!^a).?b/
# => 1
</code></pre>
<p>According to the original reporter of the linked site, this does not happen in Ruby 1.9.3.</p> Ruby master - Bug #9701 (Closed): RDoc description for `String#<<` and `String#concat`https://bugs.ruby-lang.org/issues/97012014-04-04T04:52:41Zsawa (Tsuyoshi Sawada)
<p>As far as I can see, <code>String#<<</code> is an alias of <code>String#concat</code>, but in the RDoc, there is no mentioning of the connection between them. <code>String#<<</code> should be simply described as an alias of <code>String#concat</code>.</p> Ruby master - Feature #9667 (Open): Optimization of __FILE__ and __dir__https://bugs.ruby-lang.org/issues/96672014-03-24T09:19:04Zsawa (Tsuyoshi Sawada)
<p>In the same spirit as the string literal followed by <code>freeze</code> is optimized, I think <code>__FILE__</code> and <code>__dir__</code> should be optimized. Currently, they return different object id each time they are called.</p>
<pre><code>__FILE__.object_id # => 70183725179420
__FILE__.object_id # => 70183725129020
...
</code></pre>
<p>I propose them to be optimized so that they are only created once per occurrence.</p>
<pre><code>__FILE__.object_id # => 70183725179420
__FILE__.object_id # => 70183725179420
...
</code></pre> Ruby master - Feature #9602 (Feedback): Logic with `Enumerable#grep`https://bugs.ruby-lang.org/issues/96022014-03-06T10:36:45Zsawa (Tsuyoshi Sawada)
<p><code>Enumerable#grep</code> is useful to filter things:</p>
<pre><code>[nil, {}, [], 1, :foo, "foo"].grep(String)
# => ["foo"]
</code></pre>
<ol>
<li>
<p>Often, the condition cannot be expressed as a single object on which <code>===</code> is applied, but as a disjunction over <code>===</code> applied to multiple objects. I would like <code>Enumerable#grep</code> to take arbitrary number of arguments, and when they are more than one, a logical disjunction applies, just as when there are multiple comma-separated objects after <code>when</code> in <code>case</code> condition:</p>
<pre><code> [nil, {}, [], 1, :foo, "foo"].grep(String, Symbol, Array)
# => [[], :foo, "foo"]
</code></pre>
</li>
<li>
<p>Also, it often happens that I want the negation of grep. Perhaps, <code>Enumerable#grepv</code> (<code>grepv</code> comes from <code>grep -v</code>) can be implemented as negation of <code>Enumerable#grep</code>, i.e., select elements for which <code>===</code> returns false on any of the arguments:</p>
<pre><code> [nil, {}, [], 1, :foo, "foo"].grepv(String, Symbol, Array)
# => [nil, {}, 1]
</code></pre>
</li>
</ol> Ruby master - Feature #9587 (Closed): Integer#times with optional starting valuehttps://bugs.ruby-lang.org/issues/95872014-03-02T19:52:38Zsawa (Tsuyoshi Sawada)
<p>Just like <code>Enumerator#with_index</code> takes an optional argument that specifies the initial value of the index, I would like to request that <code>Integer#times</code> take an optional argument that specifies the initial value. The usefulness of it is similar to that of <code>with_index</code> taking an argument. We sometimes want to repeat tasks a given number of times, and want to use an index not necessarily starting from <code>0</code>.</p>
<pre><code>6.times(1){|i| puts "Chapter #{i}"}
</code></pre>
<p>should give</p>
<pre><code>Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
</code></pre>
<p>with the return value <code>6</code>. We can do it with <code>1.upto(6)</code>, or with <code>#{i + 1}</code> within the block, but giving the initial value to <code>times</code> is much easier.</p> Ruby master - Bug #9581 (Closed): `=~` defined on a subclass of `String` is sometimes ignored, an...https://bugs.ruby-lang.org/issues/95812014-02-28T21:21:32Zsawa (Tsuyoshi Sawada)
<p>As is reported on StackOverflow (<a href="http://stackoverflow.com/questions/22103018" class="external">http://stackoverflow.com/questions/22103018</a>) by Gabriel, overridden <code>=~</code> on a subclass of <code>String</code> is sometimes ignored, and the original <code>String#=~</code> is called. Particularly, when we have:</p>
<pre><code>class MyString < String
def =~ re; :foo end
end
s = MyString.new("abc")
</code></pre>
<p>these give the correct result:</p>
<pre><code>r = /abc/; s =~ r # => :foo
s.send(:=~, r) # => :foo
s.send(:=~, /abc/) # => :foo
</code></pre>
<p>but in this case, <code>MyString#=~</code> is ignored, and <code>String#=~</code> is called instead:</p>
<pre><code>s =~ /abc/ # => 0
</code></pre> Ruby master - Feature #9565 (Rejected): Unifying the methods (const|class_variable|instance_varia...https://bugs.ruby-lang.org/issues/95652014-02-25T17:19:12Zsawa (Tsuyoshi Sawada)
<p>An argument to methods of the form <code>(const|class_variable|instance_variable)_(defined?|get|set)</code> already describes if it is meant to be a constant, class variable, or instance variable. For example, if <code>"Foo"</code> were to be used as an argument, a meaningful usage may be using it with <code>const_get</code>, but not <code>class_variable_get</code> or <code>instance_variable_get</code>. Whenever I use these methods, I feel redundancy and extra burden of having to repeat the information twice (once by method name and once by capitalization/sigil).</p>
<p>I propose that if we use a common word (let's say <code>token</code>, but I am not sure of this naming) in place of the words <code>const</code>, <code>class_variable</code>, and <code>instance_variable</code>, and have methods to unify them and get rid of the redundancy, then it would be easier for programmers. Particularly, <code>Object</code> should have the following instance methods that are aliases of the conventional methods:</p>
<pre><code>token_defined? ==> instance_variable_defined?
token_get ==> instance_variable_get
token_set ==> instance_variable_set
</code></pre>
<p>and <code>Module</code> should have the following instance methods that call different methods or error depending on the first argument:</p>
<pre><code>token_defined? ==> const_defined? (for capitalized arguments like "Foo")
==> class_variable_defined? (for arguments prepended with @@)
==> instance_variable_defined? (for arguments prepended with @)
==> Error (otherwise)
token_get ==> const_get (for capitalized arguments like "Foo")
==> class_variable_get (for arguments prepended with @@)
==> instance_variable_get (for arguments prepended with @)
==> Error (otherwise)
token_set ==> const_set (for capitalized arguments like "Foo")
==> class_variable_set (for arguments prepended with @@)
==> instance_variable_set (for arguments prepended with @)
==> Error (otherwise)
</code></pre>
<p>So when we use this, we do not have to think about the complicated method name; we just need to provide the right argument:</p>
<pre><code>module A
token_defined?("Foo") # => false
token_set("Foo", 1)
token_get("Foo") # => 1
token_defined?("@@foo") # => false
token_set("@@foo", 2)
token_get("@@foo") # => 2
token_defined?("@foo") # => false
token_set("@foo", 3)
token_get("@foo") # => 3
end
</code></pre> Ruby master - Feature #9557 (Open): Enumerator#next and Enumerator#peek with argumenthttps://bugs.ruby-lang.org/issues/95572014-02-23T09:14:18Zsawa (Tsuyoshi Sawada)
<p>It often happens that I want to move the current index of an enumerator by some arbitrary number <code>n</code>. <code>Enumerator#feed</code> takes the element as the argument, but that cannot be used if the enumerator has duplicate elements, or when I do not have information of a particular element to choose but just want to increment the index by some number. <code>Enumerator#next</code>, on the other hand, has a fixed value <code>1</code> to be incremented. It would be convenient if <code>Enumerator#next</code> takes an optional argument that represents the difference of the index to be incremented. The argument can be understood to be defaulted to <code>1</code> when absent.</p>
<p>Also, I often want to look not necessarily the current position, but some position away. It would be good if <code>Enumerator#peek</code> takes an optional argument that represents the positional difference to be peeked. The argument can be understood to be defaulted to <code>0</code> when absent.</p>
<pre><code>enum = [0, 1, 2, 3, 4, 5, 6, 7, 8].to_enum
enum.peek # => 0
enum.peek(0) # => 0
enum.peek(1) # => 1
enum.peek # => 0
enum.next # => 0
enum.next(1) # => 1
enum.next(2) # => 2
enum.peek # => 4
enum.peek(0) # => 4
enum.peek(1) # => 5
enum.peek # => 4
enum.next # => 4
enum.next(1) # => 5
enum.next(2) # => 6
peek # => 8
</code></pre> Ruby master - Feature #9515 (Open): `IO.each` and `CSV.each`https://bugs.ruby-lang.org/issues/95152014-02-12T16:16:41Zsawa (Tsuyoshi Sawada)
<p>In <code>IO</code> class, there are pairs of a class method and an instance method with related function:</p>
<ul>
<li>
<code>IO.read</code> and <code>IO#read</code>
</li>
<li>
<code>IO.write</code> and <code>IO#write</code>
</li>
<li>
<code>IO.foreach</code> and <code>IO#each</code> (or its alias <code>IO#each_line</code>)</li>
</ul>
<p>For consistency, please make <code>IO.each</code> an alias for <code>IO.foreach</code>.</p>
<p>The same thing can be said for <code>CSV.each</code>.</p> Ruby master - Misc #9503 (Rejected): Request for a Question and Answer sectionhttps://bugs.ruby-lang.org/issues/95032014-02-08T20:14:25Zsawa (Tsuyoshi Sawada)
<p>I often come up with a question regarding why Ruby is designed the way it is with respect to a certain specification. Without knowing why it was designed in that way, I usually start to feel that such thing should be in a different way, and I ask that as a feature on this site, but then, sometimes it is responded from the developers with a reason why it has to be the way it is.</p>
<p>When I have such question, I first try to look for answers about the design decision; especially, I am a frequent user of a Question and Answer site called StackOverflow (stackoverflow.com), and whenever I ask that kind of question, it ends up with the consensus being "Ask Matz". Afterall, only Matz (or the core developers) knows.</p>
<p>So, I request a Question and Answer section on this site regarding design decisions about Ruby: Why a certain feature was designed in such way and not in another way. I think this is the right place where such thing should be because only the developers know the reason. Then, people can ask about why a certain design decision was made before posting a feature request, and that will reduce hopeless feature requests that end up being rejected. That would be of a benefit to both the developers and the people wondering about the design.</p> Ruby master - Feature #9442 (Rejected): Multiple comparison construction with `==` and `===`https://bugs.ruby-lang.org/issues/94422014-01-23T08:23:42Zsawa (Tsuyoshi Sawada)
<p>I often want to write a condition that depends on multiple variables like this:</p>
<pre><code>if a == 1 and b == 2 then ...
elsif a == 2 and b == 1 then ...
elsif a == 2 and b == 2 then ...
...
</code></pre>
<p>In order to make this compact, I sometimes write these cases like this:</p>
<pre><code>case [a, b]
when [1, 2] then ...
when [2, 1] then ...
when [2, 2] then ...
...
</code></pre>
<p>or even</p>
<pre><code>if [a, b] == [1, 2] then ...
elsif [a, b] == [2, 1] then ...
elsif [a, b] == [2, 2] then ...
...
</code></pre>
<p>but I feel that constructing these arrays is a waste of resource, as well as is not elegant to write.</p>
<p>I request a syntax feature that allows multiple comparison using <code>==</code> and <code>===</code> in the spirit of multiple assignment with <code>=</code> :</p>
<pre><code>x, y, z = :foo, :bar, :baz
</code></pre>
<p>so that the two examples above would be respectively written as:</p>
<pre><code>case a, b
when 1, 2 then ...
when 2, 1 then ...
when 2, 2 then ...
...
</code></pre>
<p>and</p>
<pre><code>if a, b == 1, 2 then ...
elsif a, b == 2, 1 then ...
elsif a, b == 2, 2 then ...
...
</code></pre> Ruby master - Bug #9295 (Closed): `Exception#backtrace_locations` returns `nil`https://bugs.ruby-lang.org/issues/92952013-12-25T04:32:13Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
If I raise an <code>ArgumentError</code> by calling a method with wrong number of arguments, <code>Exception#backtrace_locations</code> returns <code>nil</code>, which I think is a bug:</p>
<pre><code>def foo; end
begin
foo(:bar)
rescue => e
p e.backtrace_locations
end
# => nil
</code></pre>
<p>If, instead, I raise an error manually, then it returns an array as expected:</p>
<pre><code>begin
raise ArgumentError.new
rescue => e
p e.backtrace_locations
end
# => ["this_file:2:in `<main>'"]
</code></pre>
<p>=end</p> Ruby master - Feature #9239 (Rejected): Array#to_h ignores flat arrayshttps://bugs.ruby-lang.org/issues/92392013-12-11T03:26:42Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
My understanding was that the newly introduced <code>Array#to_h</code> was to be equivalent to <code>Hash.[]</code>. But when I give a flat array, it returns an empty hash:</p>
<pre><code>[:a, :b].to_h # => {}
[:a].to_h # => {}
</code></pre>
<p>I expected <code>{:a => :b}</code> for the first example, and <code>ArgumentError: odd number of arguments for Hash</code> for the second example, as is with <code>Hash[]</code>. Was it intended to be different? If so, why is that the case? Even if it is to be different, it should at least raise an error when a flat array is given.<br>
=end</p> Ruby master - Feature #9111 (Open): Encoding-free String comparisonhttps://bugs.ruby-lang.org/issues/91112013-11-14T22:15:06Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Currently, strings with the same content but with different encodings count as different strings. This causes strange behaviour as below (noted in StackOverflow question <a href="http://stackoverflow.com/questions/19977788/strange-behavior-in-packed-ruby-strings#19978206" class="external">http://stackoverflow.com/questions/19977788/strange-behavior-in-packed-ruby-strings#19978206</a>):</p>
<pre><code>[128].pack("C") # => "\x80"
[128].pack("C") == "\x80" # => false
</code></pre>
<p>Since <code>[128].pack("C")</code> has the encoding ASCII-8BIT and <code>"\x80"</code> (by default) has the encoding UTF-8, the two strings are not equal.</p>
<p>Also, comparison of strings with different encodings may end up with a messy, unintended result.</p>
<p>I suggest that the comparison <code>String#<=></code> should not be based on the respective encoding of the strings, but all the strings should be internally converted to UTF-8 for the purpose of comparison.</p>
<p>=end</p> Ruby master - Bug #9077 (Closed): RDoc typographical errorhttps://bugs.ruby-lang.org/issues/90772013-11-05T02:50:05Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
There is a typographical error in <a href="http://www.ruby-doc.org/core-2.0.0/_lib/racc/rdoc/grammar_en_rdoc.html#label-Operator+Precedance" class="external">http://www.ruby-doc.org/core-2.0.0/_lib/racc/rdoc/grammar_en_rdoc.html#label-Operator+Precedance</a>.</p>
<pre><code>Operator Precedance => Operator Precedence
</code></pre>
<p>=end</p> Ruby master - Feature #9071 (Closed): Enumerable#slice_afterhttps://bugs.ruby-lang.org/issues/90712013-11-02T01:21:59Zsawa (Tsuyoshi Sawada)
<p>I see in this discussion: <a href="http://ruby.11.x6.nabble.com/ruby-dev-38392-Enumerable-gather-each-td3534746.html" class="external">http://ruby.11.x6.nabble.com/ruby-dev-38392-Enumerable-gather-each-td3534746.html</a> that <code>Enumerable#slice_before</code> was named as such, having in mind the possibility of <code>Enumerable#slice_after</code> being implemented in the future. I feel the former convenient, but believe the latter should be as well, and am wondering why the latter was not implemented at the same time. I request it to be implemented.</p> Ruby master - Feature #9037 (Closed): Memoization of "anonymous constants"https://bugs.ruby-lang.org/issues/90372013-10-20T15:51:19Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
I was thinking about the frozen literal argument in <a href="https://bugs.ruby-lang.org/issues/8992" class="external">https://bugs.ruby-lang.org/issues/8992</a>. I believe that the original intent for that proposal was to avoid reevaluation of specially marked string literals. I think that that idea would be best realized by memoization of a literal rather than freezing. Suppose we introduce a method <code>memoize</code> like this:</p>
<pre><code>memoize{"foo"}
</code></pre>
<p>whose block would be evaluated only the first this expression it is parsed, and always returns the return value of the block, then that would suffice for the purpose. In fact, this proposal in an extension/generalization of the arguments regarding frozen array/hash literals. The proposed syntax allows objects of an arbitrary class to be memoized, and is regardless of whether it is a literal expression. It is also independent of and is free from the difficulties regarding what to do with frozen arrays/hashes.</p>
<p>At the Ruby level, I believe there is a way to implement this by keeping a hash whose key would be something that uniquely identifies the source location of this expression. But I also believe there must be a better way at the C level.</p>
<p>If it is possible to add a keyword to the syntax, then we can have <code>memoize</code> as a keyword (of the kind similar to <code>defined?</code>) instead of a method, used like:</p>
<pre><code>memoize("foo")
</code></pre>
<p>or</p>
<pre><code>memoize "foo"
</code></pre>
<p>which might look more pleasant.</p>
<p>In either case, for Ruby 2.0 compatibility, a method <code>memoize</code> can be defined to evaluate the block or return the argument.</p>
<p>=end</p> Ruby master - Feature #8970 (Open): Array.zip and Array.producthttps://bugs.ruby-lang.org/issues/89702013-10-01T03:57:09Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Most of the time when I use <code>Array#zip</code> or <code>Array#product</code>, I feel cumbursome that I have to take out the first array and pass it as a receiver. For example, if I have</p>
<pre><code>a = [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]]
</code></pre>
<p>I have to do something like this:</p>
<pre><code>a.first.zip(*a.drop(1)){...}
a.first.product(*a.drop(1)){...}
</code></pre>
<p>Sometimes, the receiver (i.e., the first array) has significance, but most other times, that breaks asymmetry, making the code look ugly.</p>
<p>I would be happy if we had <code>Array.zip</code> and <code>Array.product</code> in addition so that we can do it like this:</p>
<pre><code>Array.zip(*a){...}
Array.product(*a){...}
</code></pre>
<p>=end</p> Ruby master - Feature #8948 (Assigned): Frozen regexhttps://bugs.ruby-lang.org/issues/89482013-09-25T04:02:37Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
I see that frozen string was accepted for Ruby 2.1, and frozen array and hash are proposed in <a href="https://bugs.ruby-lang.org/issues/8909" class="external">https://bugs.ruby-lang.org/issues/8909</a>. I feel there is even more use case for a frozen regex, i.e., a regex literal that generates a regex only once. It is frequent to have a regex within a frequently repeated portion of code, and generating the same regex each time is a waste of resource. At the moment, we can have a code like:</p>
<pre><code>class Foo
RE1 = /pattern1/
RE2 = /pattern1/
RE3 = /pattern1/
def classify
case self
when RE1 then 1
when RE2 then 2
when RE3 then 3
else 4
end
end
end
</code></pre>
<p>but suppose we have a frozen <code>Regexp</code> literal <code>//f</code>. Then we can write like:</p>
<pre><code>class Foo
def classify
case self
when /pattern1/f then 1
when /pattern1/f then 2
when /pattern1/f then 3
else 4
end
end
end
</code></pre>
<p>=end</p> Ruby master - Feature #8912 (Feedback): Exception.raisehttps://bugs.ruby-lang.org/issues/89122013-09-15T08:53:17Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
When we have a custom exception class with a custom (({initialize})) method whose arity is not (({1})):</p>
<pre><code>class MyException < StandardError
def initialize x, y
super("Something went wrong with #{x.inspect} because #{y}, blah blah")
end
end
</code></pre>
<p>in order to raise it, we have to create a new instance of it explicitly using (({new})), and embed that under (({Kernel#raise})).</p>
<pre><code>raise(MyException.new(:foo, :bar))
</code></pre>
<p>This is inconvenient, and does not look object oriented. I propose that there should be (({Exception#raise})), which is public, so that we can do:</p>
<pre><code>MyException.raise(:foo, :bar)
</code></pre>
<p>A Ruby implementation may be like this:</p>
<pre><code>class Exception
def self.raise *args; Kernel.send(:raise, *args) end
end
</code></pre>
<p>This will disallow us from calling the private method (({Kernel#raise})) (without an explicit receiver) within the context of an (({Exception})) class unless we use (({send})), but I think such use case is rare, and that should not be a problem.<br>
=end</p> Ruby master - Bug #8885 (Rejected): Incorrect time is created for time including leap secondshttps://bugs.ruby-lang.org/issues/88852013-09-10T16:13:11Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>Time.new</code> creates incorrect time when the time includes a leap second.</p>
<pre><code>Time.new(2012, 6, 30, 23, 59, 60)
# => 2012-07-01 00:00:00 +0900 # Wrong. Should be 2012-06-30 23:59:60 +0900
Time.new(2012, 6, 30, 23, 59, 60) == Time.new(2012, 7, 1, 0, 0, 0)
# => true # Wrong. Should be `false`.
</code></pre>
<p>=end</p> Ruby master - Feature #8834 (Open): Kernel#load_relativehttps://bugs.ruby-lang.org/issues/88342013-08-30T10:32:44Zsawa (Tsuyoshi Sawada)
<p>The intended difference between <code>Kernel#require</code> and <code>Kernel#load</code> is that the former is for external libraries and the latter is for Ruby scripts internal to the project. Considering this fact, <code>load</code> should be more likely than <code>require</code> to be used in a situation where you want to call a file through a relative path. Strangely, there is <code>Kernel#require_relative</code>, but no <code>Kernel#load_relative</code>. I request <code>Kernel#load_relative</code>. It is even more necessary than <code>Kernel#require_relative</code>.</p>
<p>It seems to me that people are using <code>Kernel#require_relative</code> when they want to use a relative path, even in the context where they are supposed to use <code>load</code> because of the lack of <code>Kernel#load_relative</code>. I don't think this is a good practice. Furthermore, in cases where you have a file without a <code>.rb</code> or other extention that you want to call via a relative path, there is no good way to do it.</p> Ruby master - Feature #8827 (Closed): A method that flips the receiver and the first argumenthttps://bugs.ruby-lang.org/issues/88272013-08-28T13:18:27Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
If it often happens that we need to pass to a method an argument that is the result of a long chain of operations:</p>
<pre><code>Hash[some_chain_of_operations_that_ends_up_with_an_array]
File.read(some_chain_of_operations_that_ends_up_with_a_string)
YAML.dump(some_chain_of_operations_that_ends_up_with_an_object)
...
</code></pre>
<p>I believe one basic tenet of Ruby is to encourage method chaining, but that practice is discouraged in the examples above. It would be convenient if there is a method (let us call this <code>Object#flip</code>) that flips the receiver and the first argument and sends the method so that the examples above can be written as follows:</p>
<pre><code>some_chain_of_operations_that_ends_up_with_an_array.flip(Hash, :[])
some_chain_of_operations_that_ends_up_with_a_string.flip(File, :read)
some_chain_of_operations_that_ends_up_with_an_object.flip(YAML, :dump)
...
</code></pre>
<p>The implementation in Ruby may be as follows:</p>
<pre><code>class Object
def flip receiver, method, *rest, &pr
receiver.send(method, self, *rest, &pr)
end
end
</code></pre>
<p>It would be good if we can have that as a built-in Ruby method.</p>
<p>=end</p> Ruby master - Feature #8811 (Feedback): Counterpart to `Hash#key?` for `Array`https://bugs.ruby-lang.org/issues/88112013-08-23T04:04:26Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>Hash</code> hash <code>key?</code> to tell if a key exists without checking the value. It would be convenient if there were a counterpart in <code>Array</code>. Suppose it is called <code>Array#index?</code>. Then it should behave as follows:</p>
<pre><code>[1, 2, 3].index?(2) # => true
[1, 2, 3].index?(3) # => false
[1, 2, 3].index?(-3) # => true
[1, 2, 3].index?(-4) # => false
</code></pre>
<p>This is useful when we want to insert/move/delete elements to/from a certain position of an array. Without checking if a value exists, it can be messed up. Implementing a check is cumbersome now. With the proposed method, it would become easy.</p>
<p>=end</p> Ruby master - Bug #8766 (Closed): RDoc documentation bug: Symbol literalhttps://bugs.ruby-lang.org/issues/87662013-08-10T23:04:22Zsawa (Tsuyoshi Sawada)
<p>RDoc page <a href="http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Symbols" class="external">http://www.ruby-doc.org/core-2.0/doc/syntax/literals_rdoc.html#label-Symbols</a> has a description and an example:</p>
<pre><code>Like strings, a single-quote may be used to disable interpolation:
:"my_symbol#{1 + 1}" #=> :"my_symbol\#{1 + 1}"
</code></pre>
<p>Whereas the text mentions single quote, the example has double quotes.</p> Ruby master - Feature #8765 (Feedback): Literal for symbol with interpolationhttps://bugs.ruby-lang.org/issues/87652013-08-10T22:59:23Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
I propose a symbol literal <code>%S</code> with interpolation</p>
<pre><code>foo = 3
%s{#{foo}} # => :"\#{foo}"
%S{#{foo}} # => :"3"
</code></pre>
<p>The conventional <code>:""</code> literal looks as if it is a unary operator to a string. Since there are both <code>%q</code> and <code>%Q</code> literals for string, I don't see any reason why symbols only have <code>%s</code>.<br>
=end</p> Ruby master - Feature #8614 (Open): Object#singleton_class with a blockhttps://bugs.ruby-lang.org/issues/86142013-07-10T01:04:42Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Most of the time when I use <code>Object#singleton_class</code>, I use it with <code>class_eval</code> following it, like follows:</p>
<pre><code>class Foo
singleton_class.class_eval{attr_accessor :bar}
end
</code></pre>
<p>I think it would be convenient if <code>Object#singleton_class</code> can optionally take a block so that the following will mean the same as above.</p>
<pre><code>class Foo
singleton_class{attr_accessor :bar}
end
</code></pre>
<p>=end</p> Ruby master - Bug #8607 (Closed): Stack consistency error (sp: 16, bp: 17)https://bugs.ruby-lang.org/issues/86072013-07-08T00:01:32Zsawa (Tsuyoshi Sawada)
<p>The following code:</p>
<pre><code>class A
attr_accessor :foo
end
class B
def initialize(parent)
@parent = parent
end
def method_missing(method, *args, &block)
@parent.send(method)
end
end
B.new(A.new).foo = :bar
</code></pre>
<p>returns:</p>
<pre><code>[BUG] Stack consistency error (sp: 16, bp: 17)
</code></pre>
<p>It has been reported on Stackoverflow (<a href="http://stackoverflow.com/questions/17513074/ruby-2-0-throws-bug-stack-consistency-error" class="external">http://stackoverflow.com/questions/17513074/ruby-2-0-throws-bug-stack-consistency-error</a>) by NicoSantangelo.</p> Ruby master - Feature #8563 (Rejected): Instance variable argumentshttps://bugs.ruby-lang.org/issues/85632013-06-23T04:30:51Zsawa (Tsuyoshi Sawada)
<p>Often times, people want to assign given arguments to instance variables, especially inside the method <code>initialize</code>:</p>
<pre><code class="Ruby syntaxhl" data-language="Ruby"><span class="k">def</span> <span class="nf">initialize</span> <span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">,</span> <span class="n">buz</span>
<span class="vi">@foo</span><span class="p">,</span> <span class="vi">@bar</span><span class="p">,</span> <span class="vi">@buz</span> <span class="o">=</span> <span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">,</span> <span class="n">buz</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>I propose to let method definition take instance variables as arguments so that:</p>
<pre><code class="Ruby syntaxhl" data-language="Ruby"><span class="k">def</span> <span class="nf">initialize</span> <span class="vi">@foo</span><span class="p">,</span> <span class="vi">@bar</span><span class="p">,</span> <span class="vi">@buz</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>would be equivalent as above.</p> Ruby master - Feature #8350 (Rejected): Extending `%{}` notation in string to accept an arrayhttps://bugs.ruby-lang.org/issues/83502013-05-01T02:09:08Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>String#%</code> accepts a hash as an argument and interpolates the values into <code>%{}</code>-notated portions of the string:</p>
<pre><code>"%{b} said %{a} to %{c}" % {a: "Foo", b: "Bar", c: "Baz"} #=> "Bar said Foo to Baz"
</code></pre>
<p>I would like to have it allow an array as well, where the <code>%{}</code>-notation has indices:</p>
<pre><code>"%{1} said %{0} to %{2}" % ["Foo", "Bar", "Baz"] #=> "Bar said Foo to Baz"
</code></pre>
<p>Of course, since the <code>%{}</code> notation interprets its content as a symbol, the following should also work:</p>
<pre><code>"%{1} said %{0} to %{2}" % {:"0" => "Foo", :"1" => "Bar", :"2" => "Baz"} #=> "Bar said Foo to Baz"
</code></pre>
<p>but that would not cause any conflict.<br>
There is also a different usage of <code>String#%</code> with an array argument like this:</p>
<pre><code>"%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
</code></pre>
<p>but that does not allow the array elements to appear in the string in an order different from as is in the array. The proposed feature is distinct from this, and adds flexibility.<br>
=end</p> Backport193 - Backport #8210 (Closed): Multibyte character interfering with end-line character wi...https://bugs.ruby-lang.org/issues/82102013-04-03T15:22:57Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
With this regex:</p>
<pre><code>regex1 = /\z/
</code></pre>
<p>the following strings match as expected:</p>
<pre><code>"hello" =~ regex1 # => 5
"こんにちは" =~ regex1 # => 5
</code></pre>
<p>but with these regexes:</p>
<pre><code>regex2 = /#$/?\z/
regex3 = /\n?\z/
</code></pre>
<p>they show difference:</p>
<pre><code>"hello" =~ regex2 # => 5
"hello" =~ regex3 # => 5
"こんにちは" =~ regex2 # => nil
"こんにちは" =~ regex3 # => nil
</code></pre>
<p>The string encoding is UTF-8, and the OS is Linux (i.e., <code>$/</code> is <code>"\n"</code>). I expect them to behave the same, and believe this is a bug.<br>
=end</p> Ruby master - Feature #8172 (Feedback): IndexError-returning counterparts to destructive Array me...https://bugs.ruby-lang.org/issues/81722013-03-27T10:51:46Zsawa (Tsuyoshi Sawada)
<p>There are a few desctructive <code>Array</code> methods that take an index as an argument and silently insert <code>nil</code> if the index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[];</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="ss">:foo</span><span class="p">;</span> <span class="n">a</span> <span class="c1"># => [nil, :foo]</span>
<span class="p">[].</span><span class="nf">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="ss">:foo</span><span class="p">)</span> <span class="c1"># => [nil, :foo]</span>
<span class="p">[].</span><span class="nf">fill</span><span class="p">(</span><span class="ss">:foo</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># => [nil, :foo]</span>
</code></pre>
<p>Among them, <code>Array#[]</code> has a counterpart that returns an <code>IndexError</code> when the index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[].</span><span class="nf">fetch</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># => IndexError</span>
</code></pre>
<p>and this is useful to avoid bugs that would be difficult to find if <code>Array#[]</code> were used. However for <code>Array#insert</code> and <code>Array#fill</code>, there are no such counterparts, and that fact that these methods silently insert <code>nil</code> is often the cause of a bug that is difficult to find.</p>
<p>I suggest there should be some versions of these methods that return <code>IndexError</code> when index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[].</span><span class="nf">insert!</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="ss">:foo</span><span class="p">)</span> <span class="c1"># => IndexError</span>
<span class="p">[].</span><span class="nf">fill!</span><span class="p">(</span><span class="ss">:foo</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># => IndexError</span>
</code></pre>
<p>I believe this would make debugging easier.</p> Ruby master - Bug #7803 (Closed): RDoc documentation: `Array#&` clarificationhttps://bugs.ruby-lang.org/issues/78032013-02-08T15:38:57Zsawa (Tsuyoshi Sawada)
<p>In the RDoc documentation for <code>Array#&</code>, it does not mention whether the order of the elements in the returned array preserves the order in the receiver. There is a question raised regarding this point in stackoverflow:</p>
<pre><code>http://stackoverflow.com/questions/14764135/is-order-preserved-in-arrays-when-intersection-is-done
</code></pre>
<p>and it mentions that that is included in the test for RubySpec</p>
<pre><code>https://github.com/rubyspec/rubyspec/blob/master/core/array/intersection_spec.rb#L16
</code></pre>
<p>so it looks like that is taken for granted, but it is not guaranteed. If it is indeed a feature, then that should be written in the document. If not, (meaning the order might not be preserved in a future implementation), then that should also be mentioned in the document as a warning. The documentation for <code>Array#-</code> makes that point clear:</p>
<pre><code>Array Difference---Returns a new array that is a copy of the original array, removing any items that ...
</code></pre>
<p>so, perhaps the docmentation for <code>Array#&</code> should be written along the same line.</p> Ruby master - Feature #7639 (Closed): More freedom for location of commentshttps://bugs.ruby-lang.org/issues/76392012-12-31T02:29:31Zsawa (Tsuyoshi Sawada)
<p>When we chain methods with the period at the end of each line, we can put comments in between the lines:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span><span class="o">.</span>
<span class="c1"># First, we do blah blah</span>
<span class="n">method1</span><span class="o">.</span>
<span class="c1"># Second, we do blah blah</span>
<span class="n">method2</span><span class="p">.</span>
<span class="nf">=</span><span class="k">begin</span>
<span class="no">Third</span><span class="p">,</span> <span class="n">the</span> <span class="n">following</span> <span class="n">line</span>
<span class="n">does</span> <span class="n">this</span>
<span class="ow">and</span> <span class="n">that</span> <span class="o">...</span>
<span class="o">=</span><span class="k">end</span>
<span class="n">method3</span>
</code></pre>
<p>but when we have the period at the beginning of the line, putting comments in similar positions returns an error:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="c1"># First, we do blah blah</span>
<span class="p">.</span><span class="nf">method1</span>
<span class="c1"># Second, we do blah blah</span>
<span class="p">.</span><span class="nf">method2</span>
<span class="cm">=begin
Third, the following line
does this
and that ...
=end</span>
<span class="p">.</span><span class="nf">method3</span>
<span class="c1"># => Error</span>
</code></pre>
<p>It is confusing that putting comments between lines in a method chain is sometimes allowed and sometimes not. I think it would be convenient if comments are allowed in these positions even when the following line starts with a period, and I request this as a feature. Currently, it returns an error, which means that, if such syntax were allowed, there would be no conflict with the existing syntax.</p>
<p>Furthermore, putting the period at the beginning of a line is suited for method chains because the period will visually work as bullets, and it makes more sense to have comments right before those lines.</p> Ruby master - Bug #7571 (Closed): RDoc documentation bug in Regexp#===https://bugs.ruby-lang.org/issues/75712012-12-16T10:55:54Zsawa (Tsuyoshi Sawada)
<p>RDoc documentation for `Regexp#===' says:</p>
<pre><code>Case Equality—Synonym for Regexp#=~ used in case statements.
</code></pre>
<p>but it is not a synonym for <code>Regexp#=~' as </code>Regexp#===' returns <code>true' or </code>false' and <code>Regexp#=~' return an integer or </code>nil`.</p> Ruby master - Bug #7190 (Closed): warning: already initialized constant の書式https://bugs.ruby-lang.org/issues/71902012-10-20T11:00:58Zsawa (Tsuyoshi Sawada)
<p>未定義の定数を参照した場合のエラーメッセージでは名前空間がすべて表示されます:</p>
<pre><code>module A; B; end
#=> NameError: uninitialized constant A::B
A::B
#=> NameError: uninitialized constant A::B
</code></pre>
<p>しかし、既に定義してある定数を再定義した場合の警告メッセージでは名前空間が表示されません:</p>
<pre><code>module A; B = :foo end
module A; B = :bar end
#=> warning: already initialized constant B
A::B = :baz
#=> warning: already initialized constant B
</code></pre>
<p>後者の場合も前者の場合にならって名前空間を表示した方がよいと思います。</p>
<pre><code>warning: already initialized constant A::B
</code></pre>
<p>メタプログラミングで、$stderr = StringIO.new のように警告メッセージをリダイレクトしてコード内で定数の再定義を捕捉したい場合に、名前空間なしでは問題になっている定数を再現できません。</p> Ruby master - Feature #5735 (Closed): Extending the use of splat operator to when it is inside a ...https://bugs.ruby-lang.org/issues/57352011-12-09T13:31:02Zsawa (Tsuyoshi Sawada)
<p>Ruby convention of allowing omittion of the curly brackets for the last argument is convenient:</p>
<p>foo(arg1, arg2, 1 => :a, 2 => :b)</p>
<p>Sometimes, I want to pass a hash with some modifications. For example, suppose <code>h = {3 => :c, 4 => :d, 5 => :e}</code> is a hash already defined and that I want to add some key-value pairs as well as overwrite some values of <code>h</code>, such as {3 => :c, 4 => :f, 5 => :e, 6 => :g}, and pass that. The current convention only allows:</p>
<p>foo(arg1, arg2, h.merge(4 => :f, 6 => :g))</p>
<p>but it would be more convenient if a hash preceded by the splat operator is placed in a hash, it is interpreted as part of the hash, allowing notations like:</p>
<p>foo(arg1, arg2, *h, 4 => :f, 6 => :g)</p>
<p>or, if I want to overwrite the hash <code>{4 => :f, 6 => :g}</code> with <code>h</code>, then:</p>
<p>foo(arg1, arg2, 4 => :f, 6 => :g, *h)</p>
<p>Or besides the argument position, in general, usages like the following:</p>
<p>{3 => :h, *h, 4 => :f, 6 => :g}</p>
<p>This is an analogy from the splat operator used within an array:</p>
<p>[1, 2, *[4, 5, 6], 7, 8]</p> Ruby master - Feature #5554 (Rejected): A method that applies self to a Proc if self is a Symbolhttps://bugs.ruby-lang.org/issues/55542011-11-03T03:51:47Zsawa (Tsuyoshi Sawada)
<p>Often, you want to apply a Proc to self if self is a Symbol, but not do anything if otherwise. In this case, something I call Object#desymbolize may be convenient:</p>
<pre><code>proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
</code></pre>
<p>An implementation may be as follows:</p>
<p>class Object<br>
def desymbolize; self end<br>
end<br>
class Symbol<br>
def desymbolize ≺ pr.call(self) end<br>
end</p> Ruby master - Feature #5553 (Rejected): A method for Hash that works differently depending on whe...https://bugs.ruby-lang.org/issues/55532011-11-03T03:41:07Zsawa (Tsuyoshi Sawada)
<p>A method Hash#if_key(key, [default], &pr) which works like the following will be often used, and is useful.</p>
<pre><code>a = {morning: "おはよう", daytime: "こんにちは", evening: "こんばんは", nothing: nil}
a.if_key(:morning){|str| "#{str}世界!"} #=> "おはよう世界!"
a.if_key(:nothing){|str| "#{str}世界!"} #=> "世界!"
a.if_key(:midnight){|str| "#{str}世界!"} #=> nil
a.if_key(:nothing, "どうも"){|str| "#{str}世界!"} #=> "どうも"
</code></pre>
<p>That is, when <code>key' exists, then the corresponding value will be passed to </code>pr'. Otherwise, the given `default' or the implicit default will be returned.</p> Ruby master - Feature #5552 (Rejected): Array#ljust, Array#rjusthttps://bugs.ruby-lang.org/issues/55522011-11-03T03:27:14Zsawa (Tsuyoshi Sawada)
<p>I propose the following methods Array#ljust(length [, pad]), Array#rjust(length [, pad]), whose behaviour is analogous to String#ljust(length [, padstr]), String#rjust(length [, padstr]).</p>
<pre><code>[:a, :b, :c].ljust(2) #=> [:a, :b, :c]
[:a, :b, :c].ljust(5) #=> [:a, :b, :c, nil, nil]
[:a, :b, :c].ljust(5, :x) #=> [:a, :b, :c, :x, :x]
</code></pre> Ruby master - Feature #5551 (Rejected): Enumerable#rdrophttps://bugs.ruby-lang.org/issues/55512011-11-03T03:18:24Zsawa (Tsuyoshi Sawada)
<p>There is Enumerable#drop(n), which drops elements on the left side, but there is no counterpart for dropping the right side. It will be convenient if there is Enumerable#rdrop(n) that drops the elements on the right side.</p>
<pre><code>[1, 2, 3, 4, 5].rdrop(2) # => [1, 2, 3]
</code></pre> Ruby master - Feature #5550 (Rejected): Hash#depth, Hash#flat_length for recursive hasheshttps://bugs.ruby-lang.org/issues/55502011-11-03T03:10:52Zsawa (Tsuyoshi Sawada)
<p>I often have a hash whose value is recursively a hash, which may look like the following:</p>
<pre><code>{"Japan" =>
{"Hokkaido" => "Sapporo", ...},
{"Honhuu" =>
{"Aomori" => "Hirosaki", ...},
{"Akita" => ...},
...
},
{"Shikoku" => ...},
...
}
</code></pre>
<p>In these cases, it will be convenient if there is a way to know the (maximum) depth of he original hash, and the numbers of all the "terminal nodes". I would like to propose two methods Hash#depth and Hash#flat_length, whose Ruby implementation can be as follows:</p>
<pre><code>class Hash
def depth
1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
end
def flat_length
values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
end
end
</code></pre> Ruby master - Feature #5549 (Rejected): Comparable#min, Comparable#maxhttps://bugs.ruby-lang.org/issues/55492011-11-03T02:45:19Zsawa (Tsuyoshi Sawada)
<p>I request the following methods</p>
<pre><code>Comparable#min(other)
Comparable#max(other)
</code></pre>
<p>which will work the same as</p>
<pre><code>[self, other].min
[self, other].max
</code></pre>
<p>and can be defined as</p>
<pre><code>module Comparable
def min other; [self, other].min end
def max other; [self, other].max end
end
</code></pre>
<p>Example use case: If you need to ensure that some calculated value becomes at least zero within a method chain.</p> Ruby master - Bug #4781 (Closed): Engrish in comment in Regexp#=~https://bugs.ruby-lang.org/issues/47812011-05-26T01:02:09Zsawa (Tsuyoshi Sawada)
<p>I see the following grammatical mistakes in ruby-doc, probably reflecting the comments in the source:</p>
<p>Regexp#=~</p>
<p>wrong: The assignment is not occur if the regexp is not a literal.<br>
correct: The assignment does not occur if the regexp is not a literal.</p>
<p>wrong: The assignment is not occur if the regexp is placed at right hand side.<br>
correct: The assignment does not occur if the regexp is placed at the right hand side.</p> Ruby master - Feature #4621 (Rejected): NilClass#to_hashhttps://bugs.ruby-lang.org/issues/46212011-04-27T15:33:35Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Various methods defined on <code>NilClass</code> are handy to avoid Method undefined on NilClass errors. For example,</p>
<ul>
<li>
<p><code>to_a</code> for multiple dimensioned array when there might not be some values for some indices:</p>
<pre><code> array[i].to_a[j].to_a[k]
</code></pre>
</li>
<li>
<p><code>to_s</code> for pattern match that might fail:</p>
<pre><code> string[regex].to_s*2
</code></pre>
</li>
<li>
<p><code>to_i</code>, <code>to_f</code>, <code>to_c</code> for index search that might fail, etc:</p>
<pre><code> array.index(element).to_i*3
</code></pre>
</li>
</ul>
<p>But there is no <code>NilClass#to_hash</code> even though there is <code>Hash#to_hash</code>. If such method existed (equivalent to <code>class NilClass; def to_hash; {} end end</code>), then we would be able to do:</p>
<ul>
<li>
<p><code>to_hash</code> for multiple embedded hash when there might not be some values for some keys:</p>
<pre><code> hash[:a].to_hash[:b].to_hash[:c]
</code></pre>
</li>
</ul>
<p>Is there any reason why <code>to_a</code>, <code>to_s</code>, <code>to_i</code>, <code>to_f</code>, <code>to_c</code> are all defined on <code>NilClass</code> but not for <code>to_hash</code>? If there is not, I would like to request this feature.<br>
=end</p>