Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112019-01-23T09:15:54ZRuby Issue Tracking System
Redmine Ruby master - Feature #15557 (Open): A new class that stores a condition and the previous receiverhttps://bugs.ruby-lang.org/issues/155572019-01-23T09:15:54Zsawa (Tsuyoshi Sawada)
<p>I often see code like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span> <span class="o">=</span> <span class="n">default_definition</span>
<span class="n">foo</span> <span class="o">=</span> <span class="n">some_method</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="k">if</span> <span class="n">some_condition</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="n">foo</span> <span class="o">=</span> <span class="n">another_method</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="k">if</span> <span class="n">another_condition</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="o">...</span>
</code></pre>
<p>It would be nice if we can write this as a method chain. Since we now have the method <code>then</code>, I thought it would be a nice fit to introduce a method called <code>when</code>, such that putting it right in front of <code>then</code> would execute the <code>then</code> method as ordinarily only when the condition is satisfied, and returns the previous receiver otherwise so that the code above can be rewritten as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span> <span class="o">=</span>
<span class="n">default_definition</span>
<span class="p">.</span><span class="nf">when</span><span class="p">{</span><span class="o">|</span><span class="n">foo</span><span class="o">|</span> <span class="n">some_condition</span><span class="p">(</span><span class="n">foo</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="o">|</span><span class="n">foo</span><span class="o">|</span> <span class="n">some_method</span><span class="p">(</span><span class="n">foo</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">when</span><span class="p">{</span><span class="o">|</span><span class="n">foo</span><span class="o">|</span> <span class="n">another_condition</span><span class="p">(</span><span class="n">foo</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="o">|</span><span class="n">foo</span><span class="o">|</span> <span class="n">another_method</span><span class="p">(</span><span class="n">foo</span><span class="p">)}</span>
</code></pre>
<p>This proposal is also a generalization of what I intended to cover by <a href="https://bugs.ruby-lang.org/issues/13807" class="external">https://bugs.ruby-lang.org/issues/13807</a>. That is,</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">some_condition</span> <span class="p">?</span> <span class="n">a</span> <span class="p">:</span> <span class="n">b</span>
</code></pre>
<p>would rewritten as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">when</span><span class="p">(</span><span class="o">&</span><span class="ss">:some_condition</span><span class="p">).</span><span class="nf">then</span><span class="p">{</span><span class="n">b</span><span class="p">}</span>
</code></pre>
<p>The proposal can be implemented by introducing a class called <code>Condition</code>, which stores a condition and the previous receiver, and works with <code>then</code> in a particular way.</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">when</span>
<span class="no">Condition</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="k">yield</span><span class="p">(</span><span class="nb">self</span><span class="p">))</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Condition</span>
<span class="k">def</span> <span class="nf">initialize</span> <span class="n">default</span><span class="p">,</span> <span class="n">condition</span>
<span class="vi">@default</span><span class="p">,</span> <span class="vi">@condition</span> <span class="o">=</span> <span class="n">default</span><span class="p">,</span> <span class="n">condition</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">then</span>
<span class="vi">@condition</span> <span class="p">?</span> <span class="k">yield</span><span class="p">(</span><span class="vi">@default</span><span class="p">)</span> <span class="p">:</span> <span class="vi">@default</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>And additionally, if we introduce a negated method <code>unless</code> (or <code>else</code>) as follows:</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">unless</span>
<span class="no">Condition</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="nb">self</span><span class="p">,</span> <span class="o">!</span><span class="k">yield</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 use that for purposes such as validation of a variable as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">bar</span> <span class="o">=</span>
<span class="nb">gets</span>
<span class="p">.</span><span class="nf">unless</span><span class="p">{</span><span class="o">|</span><span class="n">bar</span><span class="o">|</span> <span class="n">some_validation</span><span class="p">(</span><span class="n">bar</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="k">raise</span> <span class="s2">"The input is bad."</span><span class="p">}</span>
<span class="p">.</span><span class="nf">unless</span><span class="p">{</span><span class="o">|</span><span class="n">bar</span><span class="o">|</span> <span class="n">another_validation</span><span class="p">(</span><span class="n">bar</span><span class="p">)}</span>
<span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="k">raise</span> <span class="s2">"The input is bad in another way."</span><span class="p">}</span>
</code></pre> Ruby master - Feature #15523 (Open): Let `Range#begin` and `Range#end` be aliases of Range#first ...https://bugs.ruby-lang.org/issues/155232019-01-11T04:44:48Zsawa (Tsuyoshi Sawada)
<p>My understanding is that <code>Range#begin</code> and <code>Range#end</code>'s features are just subsets of <code>Range#first</code> and <code>Range#last</code>, respectively. And since they are slightly confusing with the keywords <code>begin</code> and <code>end</code>, I propose to either:</p>
<ul>
<li>Let <code>Range#begin</code> and <code>Range#end</code> be aliases of <code>Range#first</code> and <code>Range#last</code>, respectively, or</li>
<li>Let <code>Range#begin</code> and <code>Range#end</code> be obsolete after a migration path of waning against their use and recommending the use of <code>Range#first</code> and <code>Range#last</code> instead.</li>
</ul> 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 #13807 (Closed): A method to filter the receiver against some conditionhttps://bugs.ruby-lang.org/issues/138072017-08-11T20:38:09Zsawa (Tsuyoshi Sawada)
<p>I frequently see code that uses some value if that value satisfies a certain condition, and something else otherwise.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">some_condition</span> <span class="p">?</span> <span class="n">a</span> <span class="p">:</span> <span class="n">b</span>
</code></pre>
<p>And in most cases, the value of <code>a</code> is non-nil when the condition is satisfied.</p>
<p>I propose to have a method, perhaps named <code>verify</code>, which would implemented to be equivalent to this definition:</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">verify</span>
<span class="nb">self</span> <span class="k">if</span> <span class="k">yield</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 write the expression above (assuming <code>a</code> is non-nil when the condition is satisfied) like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">verify</span><span class="p">{</span><span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="n">a</span><span class="p">.</span><span class="nf">some_condition</span><span class="p">}</span> <span class="o">||</span> <span class="n">b</span>
</code></pre>
<p>Perhaps it would also be useful to do something like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">verify</span><span class="p">{</span><span class="o">|</span><span class="n">a</span><span class="o">|</span> <span class="n">a</span><span class="p">.</span><span class="nf">some_condition</span><span class="p">}</span><span class="o">&</span><span class="p">.</span><span class="nf">chaining_of_more_methods</span>
</code></pre> Ruby master - Feature #13314 (Open): dig=https://bugs.ruby-lang.org/issues/133142017-03-14T06:38:37Zsawa (Tsuyoshi Sawada)
<p>We have <code>Hash#dig</code>, and when we want to assign a key-value at a deep level, it is tempting to do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">hash</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="ss">:key1</span><span class="p">,</span> <span class="ss">:key2</span><span class="p">,</span> <span class="ss">:key3</span><span class="p">,</span> <span class="ss">:key4</span><span class="p">)</span> <span class="o">=</span> <span class="s2">"value
</span></code></pre>
<p>when we actually needed to do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">hash</span><span class="p">.</span><span class="nf">dig</span><span class="p">(</span><span class="ss">:key1</span><span class="p">,</span> <span class="ss">:key2</span><span class="p">,</span> <span class="ss">:key3</span><span class="p">)</span><span class="o">&</span><span class="p">.</span><span class="nf">[</span><span class="p">]</span><span class="o">=</span><span class="p">(</span><span class="ss">:key4</span><span class="p">,</span> <span class="s2">"value"</span><span class="p">)</span>
</code></pre>
<p>I propose a method <code>Hash#dig=</code>, which should be equivalent to the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Hash</span>
<span class="k">def</span> <span class="nf">dig</span><span class="o">=</span><span class="p">(</span><span class="o">*</span><span class="n">keys</span><span class="p">,</span> <span class="n">final_key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="n">dig</span><span class="p">(</span><span class="o">*</span><span class="n">keys</span><span class="p">)</span><span class="o">&</span><span class="p">.</span><span class="nf">[</span><span class="p">](</span><span class="n">final_key</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #13309 (Rejected): Locale paramter for Integer(), Float(), Rational()https://bugs.ruby-lang.org/issues/133092017-03-13T04:28:16Zsawa (Tsuyoshi Sawada)
<p>Matz' comment to my proposal on <code>Kernel#Boolean()</code> (<a href="https://bugs.ruby-lang.org/issues/13260" class="external">https://bugs.ruby-lang.org/issues/13260</a>) led me to the idea of introducing an optional locale parameter (called under the name <code>allow</code> in <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Kernel#Boolean (Rejected)" href="https://bugs.ruby-lang.org/issues/13260">#13260</a>). And now, I think that that idea can be applied generally to the <code>Kernel#Integer</code>, <code>Kernel#Float</code>, and <code>Kernel#Rational</code> methods.</p>
<p>There seems to be constant demand to parse numbers written in different languages, and there is no official clean way to handle the problem. I thought we can let these methods take an optional locale parameter to allow inputs in different languages.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Float</span><span class="p">(</span><span class="s2">"4,294,967,295.0"</span><span class="p">)</span> <span class="c1"># => 4294967295.0 English, by default</span>
<span class="no">Float</span><span class="p">(</span><span class="s2">"4,294,967,295.0"</span><span class="p">,</span> <span class="ss">locale: </span><span class="s2">"en"</span><span class="p">)</span> <span class="c1"># => 4294967295.0</span>
<span class="no">Float</span><span class="p">(</span><span class="s2">"4 294 967.295,0"</span><span class="p">,</span> <span class="ss">locale: </span><span class="s2">"de"</span><span class="p">)</span> <span class="c1"># => 4294967295.0</span>
<span class="no">Float</span><span class="p">(</span><span class="s2">"4 294 967 295,0"</span><span class="p">,</span> <span class="ss">locale: </span><span class="s2">"fr"</span><span class="p">)</span> <span class="c1"># => 4294967295.0</span>
</code></pre>
<p>Cf. <a href="https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html" class="external">https://docs.oracle.com/cd/E19455-01/806-0169/overview-9/index.html</a></p> Ruby master - Feature #13290 (Feedback): A method to use a hash like in a case constructionhttps://bugs.ruby-lang.org/issues/132902017-03-08T06:57:02Zsawa (Tsuyoshi Sawada)
<p>We often want to translate a hash into a case construction and back. For example, suppose we have a case construction like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="n">key</span>
<span class="k">when</span> <span class="sr">/foo/</span> <span class="k">then</span> <span class="s2">"FOO"</span>
<span class="k">when</span> <span class="sr">/bar/</span> <span class="k">then</span> <span class="s2">"BAR"</span>
<span class="k">else</span> <span class="s2">"DEFAULT"</span>
<span class="k">end</span>
</code></pre>
<p>Given that the keys are ordered within a hash since Ruby 1.9, the information used in the case construction above can be expressed as a hash <code>h</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{</span>
<span class="sr">/foo/</span> <span class="o">=></span> <span class="s2">"FOO"</span>
<span class="sr">/bar/</span> <span class="o">=></span> <span class="s2">"BAR"</span>
<span class="o">...</span>
<span class="p">}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">default</span> <span class="o">=</span> <span class="s2">"DEFAULT"</span>
</code></pre>
<p>At the moment, there is no straightforward way to translate the hash <code>h</code> into the case construction as above. I would like to have a method on <code>Hash</code> that takes a key and works like a case construction (i.e., applies the <code>===</code> method) as below:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span><span class="p">.</span><span class="nf">fetch_as_in_case_construction</span><span class="p">(</span><span class="s2">"barbar"</span><span class="p">)</span> <span class="c1"># => "BAR"</span>
</code></pre>
<p>The name <code>fetch_as_in_case_construction</code> is terrible. Perhaps someone can suggest a better name.</p> Ruby master - Feature #13260 (Rejected): Kernel#Booleanhttps://bugs.ruby-lang.org/issues/132602017-02-28T02:24:48Zsawa (Tsuyoshi Sawada)
<p>I think we have lots of occasions to receive a <code>true</code> or <code>false</code> value as a string input, and want to convert it to <code>true</code> or <code>false</code>. Perhaps we can have a method <code>Kernel#Boolean</code> in a similar spirit to <code>Kernel#Integer</code> and its kins, which takes an optional keyword argument <code>exception</code> (similar to <a href="https://bugs.ruby-lang.org/issues/12732" class="external">https://bugs.ruby-lang.org/issues/12732</a>) and <code>strict</code> (defaulted to true).</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Boolean</span><span class="p">(</span><span class="s2">"true"</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"false"</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">)</span> <span class="c1"># => ArgumentError</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">nil</span><span class="p">)</span> <span class="c1"># => nil</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"1"</span><span class="p">)</span> <span class="c1"># => ArgumentError</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"1"</span><span class="p">,</span> <span class="ss">strict: </span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"yes"</span><span class="p">,</span> <span class="ss">strict: </span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"0"</span><span class="p">,</span> <span class="ss">strict: </span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"no"</span><span class="p">,</span> <span class="ss">strict: </span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="no">Boolean</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="ss">strict: </span><span class="kp">false</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">nil</span><span class="p">)</span> <span class="c1"># => nil</span>
</code></pre> Ruby master - Feature #13259 (Open): Kernel#Datehttps://bugs.ruby-lang.org/issues/132592017-02-28T02:17:56Zsawa (Tsuyoshi Sawada)
<p>I often see a piece of code like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">require</span> <span class="s2">"date"</span>
<span class="k">begin</span>
<span class="no">Date</span><span class="p">.</span><span class="nf">strptime</span><span class="p">(</span><span class="n">some_string</span><span class="p">,</span> <span class="n">some_format</span><span class="p">)</span>
<span class="k">rescue</span>
<span class="kp">nil</span>
<span class="k">end</span>
</code></pre>
<p>Since we now have (<a href="https://bugs.ruby-lang.org/issues/12732" class="external">https://bugs.ruby-lang.org/issues/12732</a>) <code>Kernel#Integer</code> with a parameter to avoid raising an error in case of an invalid string, I think that having a counterpart of it in <code>Date</code> would be convenient. I propose <code>Kernel#Date</code>, which works like <code>Date.strptime</code> except that it takes an optional keyword argument, and works as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Date</span><span class="p">(</span><span class="s2">"2017/02/02"</span><span class="p">,</span> <span class="s2">"%Y/%m/%d"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">nil</span><span class="p">)</span> <span class="c1"># => #<Date: 2017-02-02 ((2457787j,0s,0n),+0s,2299161j)></span>
<span class="no">Date</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">nil</span><span class="p">)</span> <span class="c1"># => nil</span>
</code></pre> Ruby master - Bug #13162 (Rejected): tr does not recognize multi-byte characters correctlyhttps://bugs.ruby-lang.org/issues/131622017-01-27T05:33:47Zsawa (Tsuyoshi Sawada)
<p>This looks as expected:</p>
<pre><code>"\\".tr('\\', '\') # => "\"
</code></pre>
<p>but this doesn't:</p>
<pre><code>"\\".tr("\\¥'", "\¥'") # => "\\"
</code></pre>
<p>I confirmed the strings are UTF-8.</p> Ruby master - Feature #12995 (Rejected): Conditional expression taking a receiver outside the con...https://bugs.ruby-lang.org/issues/129952016-12-01T07:00:23Zsawa (Tsuyoshi Sawada)
<p>Since a conditional expression has a return value, we can continue a method chain after a conditional expression like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">if</span> <span class="n">hoge</span>
<span class="n">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">else</span>
<span class="n">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
<span class="p">.</span><span class="nf">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="n">hoge</span>
<span class="k">when</span> <span class="n">hoge1</span>
<span class="n">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">when</span> <span class="n">hoge2</span>
<span class="n">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
<span class="p">.</span><span class="nf">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span>
</code></pre>
<p>It is convenient if we can have a condition after or in the middle of a chain. I would like to do this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span><span class="p">.</span>
<span class="nf">if</span> <span class="n">hoge</span>
<span class="n">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">else</span>
<span class="n">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span>
<span class="k">if</span> <span class="n">hoge</span>
<span class="p">.</span><span class="nf">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">else</span>
<span class="p">.</span><span class="nf">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span><span class="p">.</span>
<span class="nf">case</span> <span class="n">hoge</span>
<span class="k">when</span> <span class="n">hoge1</span>
<span class="n">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">when</span> <span class="n">hoge2</span>
<span class="n">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">baz1</span><span class="p">.</span><span class="nf">baz2</span><span class="p">.</span><span class="nf">baz3</span><span class="p">.</span><span class="nf">baz4</span>
<span class="k">case</span> <span class="n">hoge</span>
<span class="k">when</span> <span class="n">hoge1</span>
<span class="p">.</span><span class="nf">foo1</span><span class="p">.</span><span class="nf">foo2</span><span class="p">.</span><span class="nf">foo3</span>
<span class="k">when</span> <span class="n">hoge2</span>
<span class="p">.</span><span class="nf">bar1</span><span class="p">.</span><span class="nf">bar2</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #12399 (Feedback): Restricted, safe version of `Kernel#eval`https://bugs.ruby-lang.org/issues/123992016-05-19T15:25:46Zsawa (Tsuyoshi Sawada)
<p><code>Kernel#eval</code> is convenient, but sometimes, it can be a security risk, and often people crazily react against using it even when it is not dangerous.</p>
<p>I propose to have a restricted version of <code>eval</code>, which can interpret Ruby literals, but whenever there is constant assignment, variable assignment, method call, or method definition, it raises an error.</p>
<p>It can be used to safely accept parameters given as a string. One example use is, parameter interpretation of command line option parser can be easily be done under the assumption that the parameter is given as Ruby expression.</p> Ruby master - Feature #12380 (Open): `Struct` as a subclass of `Class`https://bugs.ruby-lang.org/issues/123802016-05-14T06:22:27Zsawa (Tsuyoshi Sawada)
<p>This issue is somewhat of the same flavor as <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: SingletonClass (Closed)" href="https://bugs.ruby-lang.org/issues/12374">#12374</a>.</p>
<p><code>Struct</code> has a constructor that creates a class:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Struct</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:foo</span><span class="p">)</span>
<span class="c1"># => #<Class:0x007f605f892cb0></span>
</code></pre>
<p>and this is the same situation with <code>Class</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Class</span><span class="p">.</span><span class="nf">new</span>
<span class="c1"># => #<Class:0x007f605f70c788></span>
</code></pre>
<p>Hence, most naturally, <code>Struct</code> should be a subclass of <code>Class</code>. But in reality, it isn't:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Struct</span><span class="p">.</span><span class="nf">ancestors</span>
<span class="c1"># => [Struct, Enumerable, Object, Kernel, BasicObject]</span>
</code></pre>
<p>The current structure around <code>Struct</code> is counter-intuitive to me.</p>
<p>I propose that either <code>Struct</code> should be redefined as a subclass of <code>Class</code>, or a new class <code>StructClass</code> should be introduced as a subclass of <code>Class</code>, and take over the functionality of <code>Struct</code>.</p> Ruby master - Feature #12374 (Closed): SingletonClasshttps://bugs.ruby-lang.org/issues/123742016-05-12T18:28:21Zsawa (Tsuyoshi Sawada)
<p>I propose to have a class <code>SingletonClass</code>, a subclass of the class <code>Class</code>, to which all singleton classes belong. It should be the owner of all the properties that are specific to singleton classes. Also, the methods defined on <code>Singleton</code> module should be moved to this class.</p>
<p>Reasons are as follows:</p>
<ol>
<li>
<p>I was thinking that the reason <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: `Class#instance` (Closed)" href="https://bugs.ruby-lang.org/issues/12084">#12084</a> hasn't been seen positively may be because the developers do not want to define a method only on limited instances of a class. If we have <code>SingletonClass</code>, the method <code>#instance</code> proposed in <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: `Class#instance` (Closed)" href="https://bugs.ruby-lang.org/issues/12084">#12084</a> could be defined as an instance method of <code>SingletonClass</code>.</p>
</li>
<li>
<p>The way to introduce the singleton pattern using the <code>Singleton</code> module (<a href="http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html" class="external">http://ruby-doc.org/stdlib-2.3.0/libdoc/singleton/rdoc/Singleton.html</a>):</p>
</li>
</ol>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">A</span>
<span class="kp">include</span> <span class="no">Singleton</span>
<span class="c1"># ...</span>
<span class="k">end</span>
</code></pre>
<p>is a bit unnatural and verbose. If we have <code>SingletonClass</code>, then we can define a singleton class more naturally:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">A</span> <span class="o">=</span> <span class="no">SingletonClass</span><span class="p">.</span><span class="nf">new</span>
</code></pre> Ruby master - Bug #12345 (Closed): A module's private constants are given with `Module#constant(f...https://bugs.ruby-lang.org/issues/123452016-05-03T22:05:25Zsawa (Tsuyoshi Sawada)
<p>A module's private constants are given with <code>Module#constant(false)</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">A</span>
<span class="no">X</span> <span class="o">=</span> <span class="mi">1</span>
<span class="no">Y</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">private_constant</span> <span class="ss">:Y</span>
<span class="k">end</span>
<span class="k">module</span> <span class="nn">B</span>
<span class="no">Z</span> <span class="o">=</span> <span class="mi">3</span>
<span class="no">W</span> <span class="o">=</span> <span class="mi">4</span>
<span class="n">private_constant</span> <span class="ss">:W</span>
<span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">constants</span> <span class="c1"># => [:X]</span>
<span class="no">A</span><span class="p">.</span><span class="nf">constants</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => [:X, :Y]</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="p">.</span><span class="nf">constants</span> <span class="c1"># => [:X, :Z]</span>
<span class="no">A</span><span class="p">.</span><span class="nf">constants</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => [:X, :Y]</span>
</code></pre>
<p>Besides this, I request a way to get the private constants of a module. I want:</p>
<pre><code class="ruby syntaxhl" data-language="ruby">
<span class="no">A</span><span class="p">.</span><span class="nf">private_constants</span> <span class="c1"># => [:Y]</span>
<span class="no">A</span><span class="p">.</span><span class="nf">private_constants</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => [:Y]</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="p">.</span><span class="nf">private_constants</span> <span class="c1"># => [:Y, :W]</span>
<span class="no">A</span><span class="p">.</span><span class="nf">private_constants</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => [:Y]</span>
</code></pre> Ruby master - Feature #12333 (Closed): `String#concat`, `Array#concat`, `String#prepend` to take ...https://bugs.ruby-lang.org/issues/123332016-04-30T12:25:13Zsawa (Tsuyoshi Sawada)
<p>I would like <code>String#concat</code>, <code>Array#concat</code>, <code>String#prepend</code> to take multiple arguments</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">s</span> <span class="o">=</span> <span class="s2">""</span>
<span class="n">s</span><span class="p">.</span><span class="nf">concat</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="n">a</span><span class="p">.</span><span class="nf">prepend</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="n">s</span> <span class="c1"># => "ABCabc"</span>
<span class="n">a</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">concat</span><span class="p">([</span><span class="s2">"a"</span><span class="p">],</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="n">a</span> <span class="c1"># => ["a", "b", "c"]</span>
</code></pre> Ruby master - Feature #12318 (Closed): Returning the evaluated value of a blockhttps://bugs.ruby-lang.org/issues/123182016-04-26T00:53:05Zsawa (Tsuyoshi Sawada)
<p>I often achieve an element using an iterator with a block, and then apply the same/a similar block to the element I get. Examples are:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
<span class="p">.</span><span class="nf">max_by</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="p">.</span><span class="nf">tap</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="k">break</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">3</span><span class="p">}</span>
<span class="c1"># => 2</span>
<span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="s2">"baz"</span><span class="p">]</span>
<span class="p">.</span><span class="nf">find</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="sr">/(.)\1/</span><span class="p">]}</span>
<span class="p">.</span><span class="nf">tap</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="k">break</span> <span class="n">e</span><span class="p">[</span><span class="sr">/(.)\1/</span><span class="p">]}</span>
<span class="c1"># => "oo"</span>
</code></pre>
<p>I would like a method on <code>Enumerator</code> that returns the result of the block rather than the original element in the iterator. Not sure about the name, but if I call it <code>and_return</code> temporary, it should look like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">9</span><span class="p">]</span>
<span class="p">.</span><span class="nf">max_by</span><span class="p">.</span><span class="nf">and_return</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"># => 2</span>
<span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="s2">"baz"</span><span class="p">]</span>
<span class="p">.</span><span class="nf">find</span><span class="p">.</span><span class="nf">and_return</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="sr">/(.)\1/</span><span class="p">]}</span>
<span class="c1"># => "oo"</span>
</code></pre> Ruby master - Feature #12317 (Open): Name space of a modulehttps://bugs.ruby-lang.org/issues/123172016-04-26T00:15:58Zsawa (Tsuyoshi Sawada)
<p>I want a method to return the name space of a module, something like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">A</span><span class="p">;</span> <span class="k">module</span> <span class="nn">B</span><span class="p">;</span> <span class="k">module</span> <span class="nn">C</span> <span class="k">end</span> <span class="k">end</span> <span class="k">end</span>
<span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">C</span><span class="p">.</span><span class="nf">namespace</span> <span class="o">=></span> <span class="p">[</span><span class="no">A</span><span class="p">,</span> <span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="p">,</span> <span class="no">A</span><span class="o">::</span><span class="no">B</span><span class="o">::</span><span class="no">C</span><span class="p">]</span>
</code></pre>
<p>There is <code>nesting</code> method that is similar, but that only returns the lexical nesting information.</p>
<p>There are also some known hacks for this, converting the module to the string representation using <code>to_s</code> or <code>name</code>, and then splitting it by <code>::</code>. But that easily breaks if the module is anonymous, or is a singleton module. I would like a more robust, core method.</p> Ruby master - Feature #12304 (Feedback): String#split with a blockhttps://bugs.ruby-lang.org/issues/123042016-04-20T15:11:00Zsawa (Tsuyoshi Sawada)
<p>I would like <code>String#split</code> to take an optional block, similar to <code>String#scan</code> with a block.</p>
<p>Suppose I have a regex pattern with captures, and I want to iterate over matches while having access to the information in last match <code>$~</code>. In such case, I can use <code>scan</code>, and access the last match in the block during iteration:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcabd"</span><span class="p">.</span><span class="nf">scan</span><span class="p">(</span><span class="sr">/(a)|(b)/</span><span class="p">)</span> <span class="k">do</span>
<span class="o">|</span><span class="n">s</span><span class="o">|</span>
<span class="k">if</span> <span class="vg">$1</span> <span class="k">then</span> <span class="o">...</span> <span class="c1"># `s` is `"a"`, `"a"`</span>
<span class="k">elsif</span> <span class="vg">$2</span> <span class="k">then</span> <span class="o">...</span> <span class="c1"># `s` is `"b"`, `"b"`</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>In my use case, I need to iterate over not only the matching patterns, but <strong>also the non-matched substrings in between them</strong>. For such cases, I need to use <code>split</code> instead of <code>scan</code>. And I would like to do something like the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcabd"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="sr">/((a)|(b))/</span><span class="p">)</span> <span class="k">do</span>
<span class="o">|</span><span class="n">s</span><span class="o">|</span>
<span class="k">if</span> <span class="vg">$2</span> <span class="k">then</span> <span class="o">...</span> <span class="c1"># `s` is `"a"`, `"a"`</span>
<span class="k">elsif</span> <span class="vg">$3</span> <span class="k">then</span> <span class="o">...</span> <span class="c1"># `s` is `"b"`, `"b"`</span>
<span class="k">else</span> <span class="o">...</span> <span class="c1"># `s` is `"c"`, `"d"`, and `$~` should be `nil`</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>With the current functionalities, I attempted to do this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcabd"</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="sr">/((a)|(b))/</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>but it does not work the way I wanted; within the <code>each</code> block, the last match value <code>$~</code> is <code>nil</code>, and I cannot access the information.</p> Ruby master - Feature #12272 (Open): Accepting HTML entity name in string literalhttps://bugs.ruby-lang.org/issues/122722016-04-12T13:00:03Zsawa (Tsuyoshi Sawada)
<p>String literal allows the escape character <code>\u</code> to describe a character using UTF-8 character code like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"</span><span class="se">\u</span><span class="s2">201c"</span> <span class="c1"># left double quote</span>
<span class="s2">"</span><span class="se">\u</span><span class="s2">2191"</span> <span class="c1"># up arrow</span>
</code></pre>
<p>This is useful in typing characters that are not easy to input from the keyboard. However, normal people do not memorize the UTF-8 codes by heart.</p>
<p>The HTML symbol entity name is the place where we can compromise (although it is not available for the entire UTF-8), I think. I would like the string literal to be extended to accept HTML entity names and interpret them as the corresponding UTF-8 characters. I do not have a definite idea for the syntax, but a candidate can be an escape character <code>\& ... ;</code>, so that we can type:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"</span><span class="se">\&</span><span class="s2">ldquo;"</span> <span class="c1"># left double quote</span>
<span class="s2">"</span><span class="se">\&</span><span class="s2">uarr;"</span> <span class="c1"># up arrow</span>
</code></pre>
<p>Currently, <code>"\&"</code> is interpreted as <code>"&"</code>, so this will be a compatibility breaking change, and if that is not desirable, perhaps a different syntax may be considered.</p> Ruby master - Feature #12262 (Open): Anti-loophttps://bugs.ruby-lang.org/issues/122622016-04-08T07:21:19Zsawa (Tsuyoshi Sawada)
<p>The <code>loop</code> method continues by default, and requires the keyword <code>break</code> to escape. This is good when the continuing cases are the norm and the escaping cases are exceptional:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="kp">loop</span> <span class="k">do</span>
<span class="o">...</span>
<span class="k">if</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">break</span> <span class="c1"># breaks on exceptional cases</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">else</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>But when the continuing cases are exceptional and the escaping cases are the norm, the construction requires a lot of <code>break</code>, and it becomes cumbersome:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="kp">loop</span> <span class="k">do</span>
<span class="o">...</span>
<span class="k">if</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">break</span> <span class="c1"># lot of breaks</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">break</span> <span class="c1"># lot of breaks</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">break</span> <span class="c1"># lot of breaks</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">else</span>
<span class="o">...</span>
<span class="k">break</span> <span class="c1"># lot of breaks</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>I actually see this use case a lot when user input is asked with validation on a command line script.</p>
<p>I request a <code>loop</code>-like method that works in the opposite way to <code>loop</code>, that is, it escapes (i.e., runs only once) by default, and requires a keyword to continue (perhaps <code>next</code>). The second code above would then be written like:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">some_loop_like_method</span> <span class="k">do</span>
<span class="o">...</span>
<span class="k">if</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">elsif</span> <span class="o">...</span>
<span class="o">...</span>
<span class="k">next</span> <span class="c1"># continues on exceptional cases</span>
<span class="k">else</span>
<span class="o">...</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre> Ruby master - Bug #12186 (Third Party's Issue): /snapshot/lib/rubygems/installer.rb:233https://bugs.ruby-lang.org/issues/121862016-03-17T09:03:34Zsawa (Tsuyoshi Sawada)
<p>I downloaded the nightly snapshot (<a href="https://cache.ruby-lang.org/pub/ruby/snapshot.tar.gz" class="external">https://cache.ruby-lang.org/pub/ruby/snapshot.tar.gz</a>), and did:</p>
<pre><code>./configure
make
sudo make install
</code></pre>
<p>and got:</p>
<pre><code>.../snapshot/lib/rubygems/installer.rb:233:in `check_executable_overwrite': no implicit conversion of nil into String (TypeError)
</code></pre>
<p>The offending line installer.rb:233 looks like this,</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">question</span> <span class="o"><<</span> <span class="n">existing</span>
</code></pre>
<p>and the values seem to be:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">question</span> <span class="c1"># => "rake's executable \"rake\" conflicts with "</span>
<span class="n">existing</span> <span class="c1"># => nil</span>
</code></pre>
<p>Is this a bug, or is there something wrong with my environment?</p>
<hr>
<p>Also, I am looking forward for Ruby 2.3.1. Is there a place where the schedule for the release of teeny versions are announced? Or is it is not announced until release?</p> Ruby master - Bug #12174 (Rejected): Interpolation ignores `to_s` (and `inspect`) when `to_s` is ...https://bugs.ruby-lang.org/issues/121742016-03-14T17:36:01Zsawa (Tsuyoshi Sawada)
<p>When <code>to_s</code> is defined to return <code>nil</code>, interpolation prints the original inspection.</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="k">class</span> <span class="nc">A</span>
<span class="k">def</span> <span class="nf">to_s</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="no">A</span><span class="p">.</span><span class="nf">new</span><span class="si">}</span><span class="s2">"</span> <span class="c1"># => #<A:0x007f4edf19d720></span>
</code></pre>
<p>It even ignores an overwritten <code>inspect</code> definition.</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="k">class</span> <span class="nc">A</span>
<span class="k">def</span> <span class="nf">to_s</span><span class="p">;</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">inspect</span><span class="p">;</span> <span class="s2">"foo"</span> <span class="k">end</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s2">"</span><span class="si">#{</span><span class="no">A</span><span class="p">.</span><span class="nf">new</span><span class="si">}</span><span class="s2">"</span> <span class="c1"># => #<A:0x007f8176c09050></span>
</code></pre> Ruby master - Feature #12173 (Open): `Time#till_now`https://bugs.ruby-lang.org/issues/121732016-03-14T16:57:25Zsawa (Tsuyoshi Sawada)
<p>It is very frequent to have a time instance:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">t</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
</code></pre>
<p>and then after some operations, do:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="no">Time</span><span class="p">.</span><span class="nf">now</span> <span class="o">-</span> <span class="n">t</span>
</code></pre>
<p>I propose <code>Time#till_now</code>, which is equivalent to:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="k">class</span> <span class="nc">Time</span>
<span class="k">def</span> <span class="nf">till_now</span><span class="p">;</span> <span class="nb">self</span><span class="p">.</span><span class="nf">class</span><span class="p">.</span><span class="nf">now</span> <span class="o">-</span> <span class="nb">self</span> <span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>and similar methods can perhaps be defined on <code>Date</code> and <code>DateTime</code> classes as well. Another candidate for the method name is <code>until_now</code>.</p>
<p>Then we can do:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">t</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="c1"># some heavy operation</span>
<span class="nb">puts</span> <span class="s2">"It took </span><span class="si">#{</span><span class="n">t</span><span class="p">.</span><span class="nf">till_now</span><span class="si">}</span><span class="s2"> secs."</span>
</code></pre> Ruby master - Feature #12134 (Open): Comparison between `true` and `false`https://bugs.ruby-lang.org/issues/121342016-03-02T10:56:01Zsawa (Tsuyoshi Sawada)
<p>There are some needs to sort elements depending on whether they satisfy certain condition expressed as a predicate. For example, to place prime numbers before others:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="nb">require</span> <span class="s2">"prime"</span>
<span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="no">Prime</span><span class="p">.</span><span class="nf">prime?</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="p">?</span> <span class="mi">0</span> <span class="p">:</span> <span class="mi">1</span><span class="p">}</span> <span class="c1"># => [7, 5, 3, 2, 6, 4, 1]</span>
</code></pre>
<p>or to do such sort with the secondary condition to sort by the size:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="no">Prime</span><span class="p">.</span><span class="nf">prime?</span><span class="p">(</span><span class="n">e</span><span class="p">)</span> <span class="p">?</span> <span class="mi">0</span> <span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [2, 3, 5, 7, 1, 4, 6]</span>
</code></pre>
<p>Here, the temporal assignment of magic numbers <code>0</code> and <code>1</code> is ad hoc, but ordering between <code>true</code> and <code>false</code> makes sense. And given that there are <code>if</code> construction (which is unmarked case compared to the <code>unless</code> construction) and the ternary operator, in which the truthy branch is placed before the falsy branch, I think it makes sense to assume an inherent ordering of <code>true</code> being placed before <code>false</code>.</p>
<p>So I propose comparison between <code>true</code> and <code>false</code>:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="kp">true</span> <span class="o"><=></span> <span class="kp">false</span> <span class="c1"># => -1</span>
<span class="kp">false</span> <span class="o"><=></span> <span class="kp">true</span> <span class="c1"># => 1</span>
</code></pre>
<p>Using this, the cases above can be written more directly as:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="no">Prime</span><span class="p">.</span><span class="nf">prime?</span><span class="p">(</span><span class="n">e</span><span class="p">)}</span> <span class="c1"># => [7, 5, 3, 2, 6, 4, 1]</span>
<span class="p">[</span><span class="mi">7</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="no">Prime</span><span class="p">.</span><span class="nf">prime?</span><span class="p">(</span><span class="n">e</span><span class="p">),</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [2, 3, 5, 7, 1, 4, 6]</span>
</code></pre>
<hr>
<p>Please do not confuse this with the common proposal to map booleans to integers, particularly <code>true.to_i # => 1</code> and <code>false.to_i # => 0</code>. That is arbitrary, and does not make sense. In fact, my proposal goes against such proposal (under the proposal to map booleans, <code>true.to_i <=> false.to_i</code> translates to <code>1 <=> 0 # => 1</code>, which goes against my proposal <code>true <=> false # => 01</code>).</p> Ruby master - Bug #12128 (Rejected): Strings in `ARGV` are frozenhttps://bugs.ruby-lang.org/issues/121282016-02-29T18:08:15Zsawa (Tsuyoshi Sawada)
<p>It is not clear how the frozen status of strings in <code>ARGV</code> are to be described, but regardless of what I try to do (i.e. set frozen string pragma as false, which is probably irrelevant because the strings are already created at the time of file load), the strings appear frozen. I ran the following file <code>foo.rb</code> as <code>ruby foo.rb bar</code>:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="c1">#!/usr/bin/env ruby</span>
<span class="c1"># frozen_string_literal: false</span>
<span class="no">ARGV</span><span class="p">.</span><span class="nf">first</span><span class="p">.</span><span class="nf">frozen?</span> <span class="c1">#=> true</span>
<span class="no">ARGV</span><span class="p">.</span><span class="nf">first</span><span class="p">.</span><span class="nf">upcase!</span> <span class="c1">#=> can't modify frozen String (RuntimeError)</span>
</code></pre>
<p>I believe this is a bug. If not, I would like to know what determines the frozen status of the strings in <code>ARGV</code>. Is it a feature that they are always frozen?</p> Ruby master - Feature #12116 (Open): `Fixnum#divmod`, `Bignum#divmod` with multiple argumentshttps://bugs.ruby-lang.org/issues/121162016-02-26T18:04:45Zsawa (Tsuyoshi Sawada)
<p>Sometimes, I need to apply <code>divmod</code> repeatedly. For example, in order to convert a number expressing seconds into approx year, day, hour, minutes, seconds (approx in the sense of ignoring leap day and leap second), I can repeatedly apply <code>divmod</code>:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">seconds</span> <span class="o">=</span> <span class="mi">289342751</span>
<span class="n">minutes</span><span class="p">,</span> <span class="n">seconds</span> <span class="o">=</span> <span class="n">seconds</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span> <span class="c1"># => [4822379, 11]</span>
<span class="n">hours</span><span class="p">,</span> <span class="n">minutes</span> <span class="o">=</span> <span class="n">minutes</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">60</span><span class="p">)</span> <span class="c1"># => [80372, 59]</span>
<span class="n">days</span><span class="p">,</span> <span class="n">hours</span> <span class="o">=</span> <span class="n">hours</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">24</span><span class="p">)</span> <span class="c1"># => [3348, 20]</span>
<span class="n">years</span><span class="p">,</span> <span class="n">days</span> <span class="o">=</span> <span class="n">days</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">365</span><span class="p">)</span> <span class="c1"># => [9, 63]</span>
</code></pre>
<p>so that I get that 289342751 seconds is approx 9 years 63 days 20 hours 59 minutes and 11 seconds. But it is cumbersome to do all that. It would be convenient if <code>divmod</code> can take multiple arguments so that the conventional <code>divmod</code> is applied from the right-most argument to the left, returning the above result at once:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="mi">289342751</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">365</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">60</span><span class="p">,</span> <span class="mi">60</span><span class="p">)</span> <span class="c1"># => [9, 63, 20, 59, 11]</span>
</code></pre>
<p>In general, when <code>n</code> arguments are passed to the proposed <code>divmod</code>, an array of <code>n + 1</code> elements should be returned.</p>
<p>Another use case is nested arrays. Some people tend to express a matrix as a nested array, and try to access the innermost elements using multiple indices. To list the coordinates of the occurrences of <code>1</code>, one may do:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">m</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
<span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">],</span>
<span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">],</span>
<span class="p">]</span>
<span class="n">a</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">m</span><span class="p">.</span><span class="nf">each_with_index</span> <span class="k">do</span> <span class="o">|</span><span class="n">row</span><span class="p">,</span> <span class="n">i</span><span class="o">|</span>
<span class="n">row</span><span class="p">.</span><span class="nf">each_with_index</span> <span class="k">do</span> <span class="o">|</span><span class="n">e</span><span class="p">,</span> <span class="n">j</span><span class="o">|</span>
<span class="n">a</span><span class="p">.</span><span class="nf">push</span><span class="p">([</span><span class="n">i</span><span class="p">,</span> <span class="n">j</span><span class="p">])</span> <span class="k">if</span> <span class="n">e</span> <span class="o">==</span> <span class="mi">1</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">a</span> <span class="c1"># => [[0, 0], [1, 1], [2, 2]]</span>
</code></pre>
<p>But it is often easier to have a flat array and use <code>divmod</code> with it:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">m</span> <span class="o">=</span> <span class="p">[</span>
<span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
<span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span>
<span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span>
<span class="p">]</span>
<span class="n">m</span><span class="p">.</span><span class="nf">each</span><span class="p">.</span><span class="nf">with_index</span><span class="p">.</span><span class="nf">select</span><span class="p">{</span><span class="o">|</span><span class="n">e</span><span class="p">,</span> <span class="n">_</span><span class="o">|</span> <span class="n">e</span> <span class="o">==</span> <span class="mi">1</span><span class="p">}.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">_</span><span class="p">,</span> <span class="n">i</span><span class="o">|</span> <span class="n">i</span><span class="p">.</span><span class="nf">divmod</span><span class="p">(</span><span class="mi">3</span><span class="p">)}</span> <span class="c1"># => [[0, 0], [1, 1], [2, 2]]</span>
</code></pre>
<p>However, once the nesting achieves another level, it becomes cumbersome. Instead of using a nested array:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="n">t</span> <span class="o">=</span> <span class="p">[</span>
<span class="p">[</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="p">[</span><span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">],</span>
<span class="p">],</span>
<span class="p">[</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="p">[</span><span class="s2">"c"</span><span class="p">,</span> <span class="s2">"d"</span><span class="p">],</span>
<span class="p">],</span>
<span class="p">]</span>
</code></pre>
<p>one can keep using a flat array, but that would require repeated application of <code>divmod</code> to covert between the flat index and the nested index. The proposed feature would also help in such case.</p> Ruby master - Bug #12108 (Rejected): Splitting an empty string returns an empty arrayhttps://bugs.ruby-lang.org/issues/121082016-02-24T15:56:15Zsawa (Tsuyoshi Sawada)
<p>Splitting an empty string returns an empty array.</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="s2">""</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s2">""</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">""</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="s2">"a"</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">""</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="sr">//</span><span class="p">)</span> <span class="c1"># => []</span>
<span class="s2">""</span><span class="p">.</span><span class="nf">split</span><span class="p">(</span><span class="sr">/a/</span><span class="p">)</span> <span class="c1"># => []</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 - Feature #12059 (Open): `Array#single?`, `Hash#single?`https://bugs.ruby-lang.org/issues/120592016-02-09T15:37:03Zsawa (Tsuyoshi Sawada)
<p>There are some use cases when one wants to check if an array or a hash has exactly one element. I propose <code>Array#single?</code> and <code>Hash#single?</code> that checks for such cases and returns either <code>true</code> or <code>false</code>. This is an analogy from the <code>empty?</code> method on the respective class.</p>
<ul>
<li>When creating an inflectional form out of an array:</li>
</ul>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"object1"</span><span class="p">,</span> <span class="s2">"object2"</span><span class="p">]</span>
<span class="s2">"There </span><span class="si">#{</span><span class="n">a</span><span class="p">.</span><span class="nf">single</span> <span class="p">?</span> <span class="s2">"is"</span> <span class="p">:</span> <span class="s2">"are"</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">a</span><span class="p">.</span><span class="nf">length</span><span class="si">}</span><span class="s2"> </span><span class="si">#{</span><span class="n">a</span><span class="p">.</span><span class="nf">single?</span> <span class="p">?</span> <span class="s2">"object"</span> <span class="p">:</span> <span class="s2">"objects"</span><span class="si">}</span><span class="s2">."</span>
<span class="c1"># => "There are 2 objects."</span>
</code></pre>
<ul>
<li>When checking if all elements of the array are the same:</li>
</ul>
<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">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">uniq</span><span class="p">.</span><span class="nf">single?</span> <span class="c1"># => false</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="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">].</span><span class="nf">uniq</span><span class="p">.</span><span class="nf">single?</span> <span class="c1"># => true</span>
</code></pre> Ruby master - Feature #11997 (Feedback): A method to read a file with interpolationshttps://bugs.ruby-lang.org/issues/119972016-01-15T20:32:55Zsawa (Tsuyoshi Sawada)
<p>I request a single method that reads a file while interpreting interpolated Ruby code in it (in the current context). For example, suppose I have a file <code>foo.txt</code>.</p>
<p>foo.txt</p>
<pre><code>#{char * 20}
Hello, my name is #{name}.
#{char * 20}
</code></pre>
<p>I am not sure about what method name the proposed feature should have, and on which class it should be defined, but let's temporarily call it <code>File.eval</code>. Then I would like to do:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">char</span> <span class="o">=</span> <span class="s2">"*"</span>
<span class="nb">name</span> <span class="o">=</span> <span class="s2">"John Doe"</span>
<span class="no">File</span><span class="p">.</span><span class="nf">eval</span><span class="p">(</span><span class="s2">"foo.txt"</span><span class="p">)</span> <span class="c1"># => "********************\nHello, my name is John Doe.\n********************\n"</span>
</code></pre>
<p>The use case that I have in mind is to easily create template engines/preprocessors, or do an equivalent of what such software do on the fly within a script. There are tools like erb, SCSS, haml, etc., but those force a particular syntax, and they are full fledged software with their own parsing capability, which means it takes an extra time to parse, and the sortware may have their own bugs. I often feel that I do not want to adopt any of the syntax forced in those limited choice of template/preprocessing software, but do it in a freer way.</p>
<p>Using what we have currently, I can do it with:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">eval</span><span class="p">(</span><span class="s1">'"'</span> <span class="o">+</span> <span class="no">File</span><span class="p">.</span><span class="nf">read</span><span class="p">(</span><span class="s2">"foo.txt"</span><span class="p">)</span> <span class="o">+</span> <span class="s1">'"'</span><span class="p">)</span>
</code></pre>
<p>but that looks cumbersome, and wish if Ruby had a C-level built-in capability for doing that. I believe that would encourage wide range of Ruby users to more freely create their own templating system.</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 - Feature #11951 (Closed): `RubyVM::InstructionSequence.compile` should return the er...https://bugs.ruby-lang.org/issues/119512016-01-05T07:35:55Zsawa (Tsuyoshi Sawada)
<p>When <code>RubyVM::InstructionSequence.compile</code> raises a syntax error, it outputs the syntax error message to <code>$stderr</code>, and then raises a <code>SyntaxError</code> anyway, whose message is simply: <code>"compile error"</code>.</p>
<p>I don't think this is useful. For example, if I want to analyze within Ruby code whether a certain string is syntactically correct Ruby code, I would have to reasign <code>$stderr</code> to something such as <code>StringIO.new</code>, run <code>RubyVM::InstructionSequence.compile</code>, rescue the syntax error, (but instead of reading the error message from the error), read the error message by doing <code>$stderr.string</code>. This is cumbersome.</p>
<p>On the other hand, there is <code>eval</code>, which also can raise a SyntaxError. But with <code>eval</code>, the error message comes out with the raised error, and is useful. I don't see any reason why <code>RubyVM::InstructionSequence.compile</code> should behave differently from <code>eval</code> with respect to returning the error message.</p>
<p>I request <code>RubyVM::InstructionSequence.compile</code> to return the error message as the message on the raised error rather than directly printing it to <code>$stderr</code>.</p> Ruby master - Feature #11939 (Open): Syntax sugar to apply a method replace a variablehttps://bugs.ruby-lang.org/issues/119392016-01-02T13:59:28Zsawa (Tsuyoshi Sawada)
<p>There is frequent use case to modify the value of a variable and keep it referred to by the same variable name. When method chaining cannot be done (for example, when the method is conditionally called), the same variable name would have to be repeated:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">var</span> <span class="o">=</span> <span class="n">var</span><span class="p">.</span><span class="nf">some_method</span><span class="p">(</span><span class="n">some_args</span><span class="p">)</span> <span class="k">if</span> <span class="n">some_condition</span>
<span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="o">...</span>
</code></pre>
<p>I would like to propose a syntax sugar for this kind of situation. I have two options in mind.</p>
<p>(1) <code>=.</code></p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">var</span> <span class="o">=</span><span class="p">.</span><span class="nf">some_method</span><span class="p">(</span><span class="n">some_args</span><span class="p">)</span> <span class="k">if</span> <span class="n">some_condition</span>
<span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="o">...</span>
</code></pre>
<p>(2) <code>.=</code></p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="n">var</span> <span class="p">.</span><span class="nf">=</span> <span class="n">some_method</span><span class="p">(</span><span class="n">some_args</span><span class="p">)</span> <span class="k">if</span> <span class="n">some_condition</span>
<span class="n">var</span> <span class="o">=</span> <span class="o">...</span>
<span class="o">...</span>
</code></pre>
<p>Notation (2) seems to be in line with syntax sugar like <code>+=</code>, but option (1) has the advantage that the period comes together with the method name.</p> Ruby master - Bug #11871 (Closed): Heredoc unindentation with <<~ with single-quoted identifier f...https://bugs.ruby-lang.org/issues/118712015-12-25T16:20:10Zsawa (Tsuyoshi Sawada)
<p>Heredoc unindentation with <code><<~</code> works as expected when the identifier is bare or double-quoted:</p>
<pre><code>s = <<~_
fiwfewifjf
iofwejfweifwe
fjiofwejfweiof f
fiwejfweof
_
puts s
</code></pre>
<p>or</p>
<pre><code>s = <<~"_"
fiwfewifjf
iofwejfweifwe
fjiofwejfweiof f
fiwejfweof
_
puts s
</code></pre>
<p>Output:</p>
<pre><code>fiwfewifjf
iofwejfweifwe
fjiofwejfweiof f
fiwejfweof
</code></pre>
<p>But with a single-quoted identifier, the content is flushed left:</p>
<pre><code>s = <<~'_'
fiwfewifjf
iofwejfweifwe
fjiofwejfweiof f
fiwejfweof
_
puts s
</code></pre>
<p>Output:</p>
<pre><code>fiwfewifjf
iofwejfweifwe
fjiofwejfweiof f
fiwejfweof
</code></pre> Ruby master - Bug #11861 (Closed): `zip` with `&symbol` causes segmentation errorhttps://bugs.ruby-lang.org/issues/118612015-12-22T11:33:59Zsawa (Tsuyoshi Sawada)
<p>This code (<code>:foo</code> can be replaced with a different method, defined or not defined):</p>
<pre><code>[].zip([], &:foo)
</code></pre>
<p>causes a segmentation error:</p>
<pre><code>(irb):1: [BUG] Segmentation fault at 0x00000000e1112c
ruby 2.3.0preview2 (2015-12-11 trunk 53028) [x86_64-linux]
-- Control frame information -----------------------------------------------
c:0019 p:---- s:0076 e:000075 CFUNC :zip
c:0018 p:0012 s:0072 e:000071 EVAL (irb):1 [FINISH]
c:0017 p:---- s:0070 e:000069 CFUNC :eval
c:0016 p:0025 s:0063 e:000062 METHOD /usr/local/lib/ruby/2.3.0/irb/workspace.rb:86
c:0015 p:0027 s:0056 e:000054 METHOD /usr/local/lib/ruby/2.3.0/irb/context.rb:379
c:0014 p:0024 s:0050 e:000049 BLOCK /usr/local/lib/ruby/2.3.0/irb.rb:488
c:0013 p:0041 s:0042 e:000041 METHOD /usr/local/lib/ruby/2.3.0/irb.rb:622
c:0012 p:0011 s:0037 e:000036 BLOCK /usr/local/lib/ruby/2.3.0/irb.rb:485
c:0011 p:0128 s:0033 e:000032 BLOCK /usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:245 [FINISH]
c:0010 p:---- s:0030 e:000029 CFUNC :loop
c:0009 p:0009 s:0027 e:000026 BLOCK /usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:231 [FINISH]
c:0008 p:---- s:0025 e:000024 CFUNC :catch
c:0007 p:0018 s:0021 e:000020 METHOD /usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:230
c:0006 p:0037 s:0018 E:000570 METHOD /usr/local/lib/ruby/2.3.0/irb.rb:484
c:0005 p:0009 s:0015 e:000014 BLOCK /usr/local/lib/ruby/2.3.0/irb.rb:394 [FINISH]
c:0004 p:---- s:0013 e:000012 CFUNC :catch
c:0003 p:0177 s:0009 E:002630 METHOD /usr/local/lib/ruby/2.3.0/irb.rb:393
c:0002 p:0023 s:0004 E:000960 EVAL /usr/local/bin/irb:11 [FINISH]
c:0001 p:0000 s:0002 E:002480 (none) [FINISH]
-- Ruby level backtrace information ----------------------------------------
/usr/local/bin/irb:11:in `<main>'
/usr/local/lib/ruby/2.3.0/irb.rb:393:in `start'
/usr/local/lib/ruby/2.3.0/irb.rb:393:in `catch'
/usr/local/lib/ruby/2.3.0/irb.rb:394:in `block in start'
/usr/local/lib/ruby/2.3.0/irb.rb:484:in `eval_input'
/usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:230:in `each_top_level_statement'
/usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:230:in `catch'
/usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `block in each_top_level_statement'
/usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:231:in `loop'
/usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb:245:in `block (2 levels) in each_top_level_statement'
/usr/local/lib/ruby/2.3.0/irb.rb:485:in `block in eval_input'
/usr/local/lib/ruby/2.3.0/irb.rb:622:in `signal_status'
/usr/local/lib/ruby/2.3.0/irb.rb:488:in `block (2 levels) in eval_input'
/usr/local/lib/ruby/2.3.0/irb/context.rb:379:in `evaluate'
/usr/local/lib/ruby/2.3.0/irb/workspace.rb:86:in `evaluate'
/usr/local/lib/ruby/2.3.0/irb/workspace.rb:86:in `eval'
(irb):1:in `irb_binding'
(irb):1:in `zip'
-- Machine register context ------------------------------------------------
RIP: 0x00007f4f42c9eb09 RBP: 0x00007fff37becf50 RSP: 0x00007fff37bece90
RAX: 0x0000000000000000 RBX: 0x00007f4f4312ebb0 RCX: 0x00000000ffffffff
RDX: 0x0000000000e1110c RDI: 0x00007f4f4312eb50 RSI: 0x0000000000002007
R8: 0x00007f4f4438a960 R9: 0x00007f4f44325a00 R10: 0x00000000ffffffff
R11: 0x00007f4f4312ebb3 R12: 0x0000000000000000 R13: 0x0000000000000001
R14: 0x00007f4f4302f260 R15: 0x00007f4f4302f258 EFL: 0x0000000000010206
-- C level backtrace information -------------------------------------------
/usr/local/bin/ruby(rb_vm_bugreport+0x51f) [0x7f4f42e83d5f] vm_dump.c:688
/usr/local/bin/ruby(rb_bug_context+0xd0) [0x7f4f42e5fca0] error.c:423
/usr/local/bin/ruby(sigsegv+0x3e) [0x7f4f42d5c9ee] signal.c:890
/lib/x86_64-linux-gnu/libpthread.so.0 [0x7f4f4283b340]
/usr/local/bin/ruby(rb_block_arity+0x59) [0x7f4f42c9eb09] proc.c:958
/usr/local/bin/ruby(rb_ary_zip+0x176) [0x7f4f42e17326] array.c:3333
/usr/local/bin/ruby(vm_call_cfunc+0xf6) [0x7f4f42dddc96] vm_insnhelper.c:1709
/usr/local/bin/ruby(vm_call_method+0xe3) [0x7f4f42deaf23] vm_insnhelper.c:2241
/usr/local/bin/ruby(vm_exec_core+0x2026) [0x7f4f42de5786] insns.def:964
/usr/local/bin/ruby(vm_exec+0x81) [0x7f4f42de9361] vm.c:1637
/usr/local/bin/ruby(eval_string_with_cref+0x38c) [0x7f4f42dec63c] vm_eval.c:1359
/usr/local/bin/ruby(rb_f_eval+0x7f) [0x7f4f42decb0f] vm_eval.c:1398
/usr/local/bin/ruby(vm_call_cfunc+0xf6) [0x7f4f42dddc96] vm_insnhelper.c:1709
/usr/local/bin/ruby(vm_call_method+0xe3) [0x7f4f42deaf23] vm_insnhelper.c:2241
/usr/local/bin/ruby(vm_exec_core+0x20f9) [0x7f4f42de5859] insns.def:995
/usr/local/bin/ruby(vm_exec+0x81) [0x7f4f42de9361] vm.c:1637
/usr/local/bin/ruby(loop_i+0x14f) [0x7f4f42dec01f] vm.c:961
/usr/local/bin/ruby(rb_rescue2+0xc9) [0x7f4f42c97669] eval.c:799
/usr/local/bin/ruby(vm_call_cfunc+0xf6) [0x7f4f42dddc96] vm_insnhelper.c:1709
/usr/local/bin/ruby(vm_call_method+0xe3) [0x7f4f42deaf23] vm_insnhelper.c:2241
/usr/local/bin/ruby(vm_exec_core+0x2026) [0x7f4f42de5786] insns.def:964
/usr/local/bin/ruby(vm_exec+0x81) [0x7f4f42de9361] vm.c:1637
/usr/local/bin/ruby(catch_i+0x177) [0x7f4f42debd07] vm.c:961
/usr/local/bin/ruby(rb_catch_protect+0xaf) [0x7f4f42de061f] vm_eval.c:2013
/usr/local/bin/ruby(rb_f_catch+0x31) [0x7f4f42de07b1] vm_eval.c:1992
/usr/local/bin/ruby(vm_call_cfunc+0xf6) [0x7f4f42dddc96] vm_insnhelper.c:1709
/usr/local/bin/ruby(vm_call_method+0xe3) [0x7f4f42deaf23] vm_insnhelper.c:2241
/usr/local/bin/ruby(vm_exec_core+0x2026) [0x7f4f42de5786] insns.def:964
/usr/local/bin/ruby(vm_exec+0x81) [0x7f4f42de9361] vm.c:1637
/usr/local/bin/ruby(catch_i+0x177) [0x7f4f42debd07] vm.c:961
/usr/local/bin/ruby(rb_catch_protect+0xaf) [0x7f4f42de061f] vm_eval.c:2013
/usr/local/bin/ruby(rb_f_catch+0x31) [0x7f4f42de07b1] vm_eval.c:1992
/usr/local/bin/ruby(vm_call_cfunc+0xf6) [0x7f4f42dddc96] vm_insnhelper.c:1709
/usr/local/bin/ruby(vm_call_method+0xe3) [0x7f4f42deaf23] vm_insnhelper.c:2241
/usr/local/bin/ruby(vm_exec_core+0x2026) [0x7f4f42de5786] insns.def:964
/usr/local/bin/ruby(vm_exec+0x81) [0x7f4f42de9361] vm.c:1637
/usr/local/bin/ruby(ruby_exec_internal+0xc4) [0x7f4f42c930e4] eval.c:244
/usr/local/bin/ruby(ruby_run_node+0x2d) [0x7f4f42c96c4d] eval.c:309
/usr/local/bin/ruby(main+0x4b) [0x7f4f42c92d4b] addr2line.c:179
-- Other runtime information -----------------------------------------------
* Loaded script: irb
* Loaded features:
0 enumerator.so
1 thread.rb
2 rational.so
3 complex.so
4 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/encdb.so
5 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/trans/transdb.so
6 /usr/local/lib/ruby/2.3.0/unicode_normalize.rb
7 /usr/local/lib/ruby/2.3.0/x86_64-linux/rbconfig.rb
8 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/compatibility.rb
9 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/defaults.rb
10 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/deprecate.rb
11 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/errors.rb
12 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/version.rb
13 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/requirement.rb
14 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/platform.rb
15 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/basic_specification.rb
16 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb
17 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/util/list.rb
18 /usr/local/lib/ruby/2.3.0/x86_64-linux/stringio.so
19 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb
20 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/exceptions.rb
21 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_gem.rb
22 /usr/local/lib/ruby/2.3.0/monitor.rb
23 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb
24 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems.rb
25 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/path_support.rb
26 /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/dependency.rb
27 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/version.rb
28 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/core_ext/name_error.rb
29 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/levenshtein.rb
30 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/jaro_winkler.rb
31 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkable.rb
32 /usr/local/lib/ruby/2.3.0/delegate.rb
33 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkers/name_error_checkers/class_name_checker.rb
34 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkers/name_error_checkers/variable_name_checker.rb
35 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkers/name_error_checkers.rb
36 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkers/method_name_checker.rb
37 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/spell_checkers/null_checker.rb
38 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean/formatter.rb
39 /usr/local/lib/ruby/gems/2.3.0/gems/did_you_mean-1.0.0.rc1/lib/did_you_mean.rb
40 /usr/local/lib/ruby/2.3.0/e2mmap.rb
41 /usr/local/lib/ruby/2.3.0/irb/init.rb
42 /usr/local/lib/ruby/2.3.0/irb/workspace.rb
43 /usr/local/lib/ruby/2.3.0/irb/inspector.rb
44 /usr/local/lib/ruby/2.3.0/irb/context.rb
45 /usr/local/lib/ruby/2.3.0/irb/extend-command.rb
46 /usr/local/lib/ruby/2.3.0/irb/output-method.rb
47 /usr/local/lib/ruby/2.3.0/irb/notifier.rb
48 /usr/local/lib/ruby/2.3.0/irb/slex.rb
49 /usr/local/lib/ruby/2.3.0/irb/ruby-token.rb
50 /usr/local/lib/ruby/2.3.0/irb/ruby-lex.rb
51 /usr/local/lib/ruby/2.3.0/irb/src_encoding.rb
52 /usr/local/lib/ruby/2.3.0/irb/magic-file.rb
53 /usr/local/lib/ruby/2.3.0/x86_64-linux/readline.so
54 /usr/local/lib/ruby/2.3.0/irb/input-method.rb
55 /usr/local/lib/ruby/2.3.0/irb/locale.rb
56 /usr/local/lib/ruby/2.3.0/irb.rb
* Process memory map:
7f4f3f744000-7f4f405c3000 r--s 00000000 08:03 1441793 /usr/local/bin/ruby
7f4f405c3000-7f4f405d9000 r-xp 00000000 08:03 3014702 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f4f405d9000-7f4f407d8000 ---p 00016000 08:03 3014702 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f4f407d8000-7f4f407d9000 rw-p 00015000 08:03 3014702 /lib/x86_64-linux-gnu/libgcc_s.so.1
7f4f407d9000-7f4f407fe000 r-xp 00000000 08:03 3015032 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f4f407fe000-7f4f409fd000 ---p 00025000 08:03 3015032 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f4f409fd000-7f4f40a01000 r--p 00024000 08:03 3015032 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f4f40a01000-7f4f40a02000 rw-p 00028000 08:03 3015032 /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f4f40a02000-7f4f40a3f000 r-xp 00000000 08:03 3014923 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f4f40a3f000-7f4f40c3f000 ---p 0003d000 08:03 3014923 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f4f40c3f000-7f4f40c41000 r--p 0003d000 08:03 3014923 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f4f40c41000-7f4f40c47000 rw-p 0003f000 08:03 3014923 /lib/x86_64-linux-gnu/libreadline.so.6.3
7f4f40c47000-7f4f40c48000 rw-p 00000000 00:00 0
7f4f40c48000-7f4f40c4f000 r-xp 00000000 08:03 1628302 /usr/local/lib/ruby/2.3.0/x86_64-linux/readline.so
7f4f40c4f000-7f4f40e4f000 ---p 00007000 08:03 1628302 /usr/local/lib/ruby/2.3.0/x86_64-linux/readline.so
7f4f40e4f000-7f4f40e50000 r--p 00007000 08:03 1628302 /usr/local/lib/ruby/2.3.0/x86_64-linux/readline.so
7f4f40e50000-7f4f40e51000 rw-p 00008000 08:03 1628302 /usr/local/lib/ruby/2.3.0/x86_64-linux/readline.so
7f4f40e51000-7f4f40e58000 r-xp 00000000 08:03 1628305 /usr/local/lib/ruby/2.3.0/x86_64-linux/stringio.so
7f4f40e58000-7f4f41058000 ---p 00007000 08:03 1628305 /usr/local/lib/ruby/2.3.0/x86_64-linux/stringio.so
7f4f41058000-7f4f41059000 r--p 00007000 08:03 1628305 /usr/local/lib/ruby/2.3.0/x86_64-linux/stringio.so
7f4f41059000-7f4f4105a000 rw-p 00008000 08:03 1628305 /usr/local/lib/ruby/2.3.0/x86_64-linux/stringio.so
7f4f4105a000-7f4f4105c000 r-xp 00000000 08:03 1755447 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/trans/transdb.so
7f4f4105c000-7f4f4125c000 ---p 00002000 08:03 1755447 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/trans/transdb.so
7f4f4125c000-7f4f4125d000 r--p 00002000 08:03 1755447 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/trans/transdb.so
7f4f4125d000-7f4f4125e000 rw-p 00003000 08:03 1755447 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/trans/transdb.so
7f4f4125e000-7f4f41260000 r-xp 00000000 08:03 1755438 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/encdb.so
7f4f41260000-7f4f4145f000 ---p 00002000 08:03 1755438 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/encdb.so
7f4f4145f000-7f4f41460000 r--p 00001000 08:03 1755438 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/encdb.so
7f4f41460000-7f4f41461000 rw-p 00002000 08:03 1755438 /usr/local/lib/ruby/2.3.0/x86_64-linux/enc/encdb.so
7f4f41461000-7f4f41d23000 r--p 00000000 08:03 1183304 /usr/lib/locale/locale-archive
7f4f41d23000-7f4f41ede000 r-xp 00000000 08:03 3034899 /lib/x86_64-linux-gnu/libc-2.19.so
7f4f41ede000-7f4f420dd000 ---p 001bb000 08:03 3034899 /lib/x86_64-linux-gnu/libc-2.19.so
7f4f420dd000-7f4f420e1000 r--p 001ba000 08:03 3034899 /lib/x86_64-linux-gnu/libc-2.19.so
7f4f420e1000-7f4f420e3000 rw-p 001be000 08:03 3034899 /lib/x86_64-linux-gnu/libc-2.19.so
7f4f420e3000-7f4f420e8000 rw-p 00000000 00:00 0
7f4f420e8000-7f4f421ed000 r-xp 00000000 08:03 3014710 /lib/x86_64-linux-gnu/libm-2.19.so
7f4f421ed000-7f4f423ec000 ---p 00105000 08:03 3014710 /lib/x86_64-linux-gnu/libm-2.19.so
7f4f423ec000-7f4f423ed000 r--p 00104000 08:03 3014710 /lib/x86_64-linux-gnu/libm-2.19.so
7f4f423ed000-7f4f423ee000 rw-p 00105000 08:03 3014710 /lib/x86_64-linux-gnu/libm-2.19.so
7f4f423ee000-7f4f423f7000 r-xp 00000000 08:03 3014789 /lib/x86_64-linux-gnu/libcrypt-2.19.so
7f4f423f7000-7f4f425f7000 ---p 00009000 08:03 3014789 /lib/x86_64-linux-gnu/libcrypt-2.19.so
7f4f425f7000-7f4f425f8000 r--p 00009000 08:03 3014789 /lib/x86_64-linux-gnu/libcrypt-2.19.so
7f4f425f8000-7f4f425f9000 rw-p 0000a000 08:03 3014789 /lib/x86_64-linux-gnu/libcrypt-2.19.so
7f4f425f9000-7f4f42627000 rw-p 00000000 00:00 0
7f4f42627000-7f4f4262a000 r-xp 00000000 08:03 3014763 /lib/x86_64-linux-gnu/libdl-2.19.so
7f4f4262a000-7f4f42829000 ---p 00003000 08:03 3014763 /lib/x86_64-linux-gnu/libdl-2.19.so
7f4f42829000-7f4f4282a000 r--p 00002000 08:03 3014763 /lib/x86_64-linux-gnu/libdl-2.19.so
7f4f4282a000-7f4f4282b000 rw-p 00003000 08:03 3014763 /lib/x86_64-linux-gnu/libdl-2.19.so
7f4f4282b000-7f4f42844000 r-xp 00000000 08:03 3034900 /lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f42844000-7f4f42a43000 ---p 00019000 08:03 3034900 /lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f42a43000-7f4f42a44000 r--p 00018000 08:03 3034900 /lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f42a44000-7f4f42a45000 rw-p 00019000 08:03 3034900 /lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f42a45000-7f4f42a49000 rw-p 00000000 00:00 0
7f4f42a49000-7f4f42a6c000 r-xp 00000000 08:03 3015621 /lib/x86_64-linux-gnu/ld-2.19.so
7f4f42aa9000-7f4f42c6b000 r--s 00000000 08:03 3034899 /lib/x86_64-linux-gnu/libc-2.19.so
7f4f42c6b000-7f4f42c6c000 r--p 00022000 08:03 3015621 /lib/x86_64-linux-gnu/ld-2.19.so
7f4f42c6c000-7f4f42c6d000 rw-p 00023000 08:03 3015621 /lib/x86_64-linux-gnu/ld-2.19.so
7f4f42c6d000-7f4f42c6e000 rw-p 00000000 00:00 0
7f4f42c6e000-7f4f42f55000 r-xp 00000000 08:03 1441793 /usr/local/bin/ruby
7f4f42f65000-7f4f4300c000 r--s 00000000 08:03 1198073 /usr/lib/debug/lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f4300c000-7f4f4302f000 r--s 00000000 08:03 3034900 /lib/x86_64-linux-gnu/libpthread-2.19.so
7f4f4302f000-7f4f43134000 rw-p 00000000 00:00 0
7f4f43144000-7f4f43147000 rw-p 00000000 00:00 0
7f4f43147000-7f4f4314e000 r--s 00000000 08:03 1457857 /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f4f4314e000-7f4f4314f000 ---p 00000000 00:00 0
7f4f4314f000-7f4f43154000 rw-p 00000000 00:00 0 [stack:7161]
7f4f43154000-7f4f43159000 r--p 002e6000 08:03 1441793 /usr/local/bin/ruby
7f4f43159000-7f4f4315a000 rw-p 002eb000 08:03 1441793 /usr/local/bin/ruby
7f4f4315a000-7f4f4316c000 rw-p 00000000 00:00 0
7f4f44324000-7f4f44856000 rw-p 00000000 00:00 0 [heap]
7fff373a2000-7fff37bf1000 rw-p 00000000 00:00 0
7fff37bf4000-7fff37bf6000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
</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 #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 #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 #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 - 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 #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 #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 #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 - 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 - 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 - 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 - 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 #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 #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 - 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 - 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 #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>