Ruby Issue Tracking System: Issueshttps://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112023-06-17T14:53:51ZRuby Issue Tracking System
Redmine Ruby master - Feature #19734 (Rejected): Let `Dir` methods be available to `File`https://bugs.ruby-lang.org/issues/197342023-06-17T14:53:51Zsawa (Tsuyoshi Sawada)
<p>I propose to let <code>Dir</code> singleton methods be available to <code>File</code> in some way. Motivations are as follows.</p>
<p>When we want to do something with a file (for example <code>File.read</code> or <code>File.write</code>), we quite often want to achieve the home directory. If we are in the mode of thinking in the ream of <code>File</code>, we would likely come up with <code>File.expand_path("~")</code>. However, this is cumbersome, and actually, <code>Dir.home</code> is simpler. But my experience is that switching the mind to <code>Dir</code> when thinking about <code>File</code> require extra cognitive load.</p>
<p>Also, many methods defined in <code>File</code> work for both a file or a directory. When we are doing something with a directory, we have to stop and think whether the method we want to use is defined in <code>File</code> or in <code>Dir</code>. It is possible to do that, but that also requires extra cognitive load.</p>
<p>I thought things would become simpler if we do not need to switch between these two classes in mind, and keep using <code>File</code>. There are a few singleton methods that are defined on both <code>File</code> and <code>Dir</code>:</p>
<pre><code>delete/unlink, empty?, exist?, new/open
</code></pre>
<p>For them, perhaps some modification can be made.</p> Ruby master - Bug #19733 (Feedback): Kernel#Rational does not accept prefix 0https://bugs.ruby-lang.org/issues/197332023-06-15T00:44:52Zsawa (Tsuyoshi Sawada)
<p><code>Integer</code> and <code>Rational</code> literals accept prefix <code>0</code>. There is no difference in this respect.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mb">0b10</span> <span class="c1"># => 2</span>
<span class="mb">0b10</span><span class="n">r</span> <span class="c1"># => (2/1)</span>
</code></pre>
<p>However, when it comes to <code>Kernel#Integer</code> and <code>Kernel#Rational</code>, the former accepts prefix <code>0</code> while the latter does not. This is confusing. And as I do not see any reason they should behave differently, I think it is a bug.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Integer</span><span class="p">(</span><span class="s2">"0b10"</span><span class="p">)</span> <span class="c1"># => 2</span>
<span class="no">Rational</span><span class="p">(</span><span class="s2">"0b10"</span><span class="p">)</span> <span class="c1"># !> ArgumentError</span>
</code></pre> Ruby master - Feature #19559 (Rejected): Introduce `Symbol#+@` and `Symbol#-@`, and eventually re...https://bugs.ruby-lang.org/issues/195592023-03-30T05:23:38Zsawa (Tsuyoshi Sawada)
<p>I propose to define <code>Symbol#+@</code> and <code>Symbol#-@</code>, so that we can add positive or negative polarity to symbols. A possible implementation can be equivalent to what can be achieved by this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Symbol</span>
<span class="k">def</span> <span class="nf">-@</span><span class="p">;</span> <span class="s2">"-</span><span class="si">#{</span><span class="nb">self</span><span class="si">}</span><span class="s2">"</span><span class="p">.</span><span class="nf">to_sym</span> <span class="k">end</span>
<span class="k">def</span> <span class="nf">+@</span><span class="p">;</span> <span class="nb">self</span> <span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>The intention behind this is to, eventually, replace boolean positional or keyword arguments with symbols so that, instead of this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"foo"</span><span class="p">.</span><span class="nf">methods</span><span class="p">(</span><span class="kp">false</span><span class="p">)</span>
<span class="nb">gets</span><span class="p">(</span><span class="ss">chomp: </span><span class="kp">true</span><span class="p">)</span>
<span class="no">Integer</span><span class="p">(</span><span class="s2">"2.3"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span>
</code></pre>
<p>we can write like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"foo"</span><span class="p">.</span><span class="nf">methods</span><span class="p">(</span><span class="o">-</span><span class="ss">:inherit</span><span class="p">)</span>
<span class="nb">gets</span><span class="p">(</span><span class="o">+</span><span class="ss">:chomp</span><span class="p">)</span>
<span class="no">Integer</span><span class="p">(</span><span class="s2">"2.3"</span><span class="p">,</span> <span class="o">-</span><span class="ss">:exception</span><span class="p">)</span>
</code></pre> Ruby master - Misc #19111 (Closed): Issues with "feedback" status are tagged as "closedhttps://bugs.ruby-lang.org/issues/191112022-11-08T04:15:18Zsawa (Tsuyoshi Sawada)
<p>I see that the issues under the status "feedback" are tagged as "closed" in the title and in the list. Is this a bug or an intended setting? <a href="https://www.redmineup.com/pages/help/redmine/set-issue-statuses#:~:text=Predefined%20Issue%20statuses%20in%20Redmine,means%20%E2%80%9Copen%E2%80%9D%20issues%20category." class="external">Redmine's document</a> says (bold face mine):</p>
<blockquote>
<p>Predefined Issue statuses in Redmine are New, In progress, Resolved, Feedback, Closed, and Rejected. Statuses marked for Issue closed belong to “closed” issues category; <strong>any other status means “open” issues category</strong>.</p>
</blockquote> Ruby master - Feature #19069 (Rejected): Default value assignment with `Hash.new` in block formhttps://bugs.ruby-lang.org/issues/190692022-10-18T11:58:14Zsawa (Tsuyoshi Sawada)
<p>This is a spin-out from <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Hash.new with non-value objects should be less confusing (Rejected)" href="https://bugs.ruby-lang.org/issues/19063">#19063</a>, and is a recapture of my comment <a href="https://bugs.ruby-lang.org/issues/19063#note-15" class="external">https://bugs.ruby-lang.org/issues/19063#note-15</a>.</p>
<p>I propose to change the behavior of <code>Hash.new</code> when it takes a block with its parameter signature absent. In such case, the evaluated value of the block should be assigned to the hash with the missing key in question (<code>h3</code> below). When the block does take parameters, then the behavior should remain as is now (<code>h1</code>, <code>h2</code> below).</p>
<pre><code class="rb syntaxhl" data-language="rb"><span class="n">h1</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="n">h</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"foo"</span><span class="p">}</span>
<span class="n">h2</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="s2">"foo"</span><span class="p">}</span>
<span class="n">h3</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="s2">"foo"</span><span class="p">}</span>
<span class="n">h1</span><span class="p">[</span><span class="ss">:a</span><span class="p">]</span> <span class="c1"># => "foo"</span>
<span class="n">h2</span><span class="p">[</span><span class="ss">:a</span><span class="p">]</span> <span class="c1"># => "foo"</span>
<span class="n">h3</span><span class="p">[</span><span class="ss">:a</span><span class="p">]</span> <span class="c1"># => "foo"</span>
<span class="n">h1</span> <span class="c1"># => {:a=>"foo"}</span>
<span class="n">h2</span> <span class="c1"># => {}</span>
<span class="n">h3</span> <span class="c1"># => {:a=>"foo"}</span>
</code></pre>
<p>This will solve a few problems. First, as discussed in <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Hash.new with non-value objects should be less confusing (Rejected)" href="https://bugs.ruby-lang.org/issues/19063">#19063</a>, many users make the mistake of writing <code>Hash.new([])</code> when they actually mean <code>Hash.new{|h, k| h[k] = []}</code>, and I suspect this is partially due to the fact that the block <code>{|h, k| h[k] = []}</code> is too long, and the users are tempted to avoid writing so. With the proposed feature introduced, the users will be encouraged to naturally write the correct form <code>Hash.new{[]}</code>.</p>
<p>Second, some of the more advanced users than those mentioned above still make the mistake of writing <code>Hash.new{foo}</code> when they actually mean <code>Hash.new{|h, k| h[k] = foo}</code>, and the current proposal is to let them actually be equivalent.</p>
<p>Third, this will resemble a similar construct <code>Array.new(5){[]}</code> and they will make a good parallel.</p>
<p>Indeed, there are situations where the intended behavior is to just run a routine without assigning a new key-value pair to the hash. Examples of current code may be as follows:</p>
<pre><code class="rb syntaxhl" data-language="rb"><span class="n">h4</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="n">some_routine_to_take_care_of_the_missing_key_situation</span><span class="p">}</span>
<span class="n">h5</span> <span class="o">=</span> <span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="k">raise</span> <span class="no">WrongKeyBlahBlahError</span><span class="p">}</span>
</code></pre>
<p>Such blocks would need to be prepended by a parameter signature <code>|h, k|</code> in order to avoid unwanted results. I do not think such transition would be a huge pain.</p> Ruby master - Feature #18690 (Open): Allow `Kernel#then` to take argumentshttps://bugs.ruby-lang.org/issues/186902022-04-12T09:20:21Zsawa (Tsuyoshi Sawada)
<p><code>Kernel#then</code> passes the receiver to the block as its first positional block parameter.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">1.5</span><span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="no">Math</span><span class="p">.</span><span class="nf">atan</span><span class="p">(</span><span class="n">x</span><span class="p">)}</span>
</code></pre>
<p>I would like to propose to let <code>then</code> take arguments, which would be passed to the block as the other block parameters.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mi">3</span><span class="p">.</span><span class="nf">then</span><span class="p">(</span><span class="mi">4</span><span class="p">){</span><span class="o">|</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="o">|</span> <span class="no">Math</span><span class="p">.</span><span class="nf">hypot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">)}</span>
</code></pre>
<p>There are two uses. First, to separate bulky or repeated parameters from the routine. Instead of writing:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">honyarara</span><span class="p">.</span><span class="nf">then</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span>
<span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">bar</span><span class="p">(</span><span class="n">fugafugafuga</span><span class="p">)</span>
<span class="n">baz</span><span class="p">(</span><span class="n">hogehogehoge</span><span class="p">)</span>
<span class="n">qux</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">fugafugafuga</span><span class="p">,</span> <span class="n">hogehogehoge</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<p>we can then write:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">honyarara</span><span class="p">.</span><span class="nf">then</span><span class="p">(</span><span class="n">fugafugafuga</span><span class="p">,</span> <span class="n">hogehogehoge</span><span class="p">){</span><span class="o">|</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="o">|</span>
<span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">bar</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">baz</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">qux</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
<span class="p">}</span>
</code></pre>
<p>Second, to use a proc with multiple parameters when, for some reason, you do not want to define a method to do it:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">p</span> <span class="o">=</span> <span class="o">-></span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">){</span>
<span class="n">foo</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">bar</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
<span class="n">baz</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">qux</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">z</span><span class="p">)</span>
<span class="p">}</span>
<span class="n">honyarara</span><span class="p">.</span><span class="nf">then</span><span class="p">(</span><span class="n">fugafugafuga</span><span class="p">,</span> <span class="n">hogehogehoge</span><span class="p">,</span> <span class="o">&</span><span class="nb">p</span><span class="p">)</span>
</code></pre> Ruby master - Feature #18366 (Rejected): Enumerator#return_evalhttps://bugs.ruby-lang.org/issues/183662021-11-29T04:04:05Zsawa (Tsuyoshi Sawada)
<p>Some <code>Enumerable</code> methods return one or more of the receiver's elements according to the return value of a block it takes. Often, we want such evaluated value rather than the original element.</p>
<p>For example, suppose we want to know the character width sufficient to fit all the strings in an array:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="s2">"Hello"</span><span class="p">,</span> <span class="s2">"my"</span><span class="p">,</span> <span class="s2">"name"</span><span class="p">,</span> <span class="s2">"is"</span><span class="p">,</span> <span class="s2">"Ruby"</span><span class="p">]</span>
</code></pre>
<p>We either have to repeat the evaluation of the block:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">max_by</span><span class="p">(</span><span class="o">&</span><span class="ss">:length</span><span class="p">).</span><span class="nf">length</span> <span class="c1"># => 5</span>
</code></pre>
<p>or create a temporal array:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:length</span><span class="p">).</span><span class="nf">max</span> <span class="c1"># => 5</span>
</code></pre>
<p>both of which seem not to be optimal.</p>
<p>I propose to have a method <code>Enumerator#return_eval</code> that returns the evaluated value(s) of the block:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">max_by</span><span class="p">.</span><span class="nf">return_eval</span><span class="p">(</span><span class="o">&</span><span class="ss">:length</span><span class="p">)</span> <span class="c1"># => 5</span>
<span class="n">a</span><span class="p">.</span><span class="nf">min_by</span><span class="p">.</span><span class="nf">return_eval</span><span class="p">(</span><span class="o">&</span><span class="ss">:length</span><span class="p">)</span> <span class="c1"># => 2</span>
<span class="n">a</span><span class="p">.</span><span class="nf">minmax_by</span><span class="p">.</span><span class="nf">return_eval</span><span class="p">(</span><span class="o">&</span><span class="ss">:length</span><span class="p">)</span> <span class="c1"># => [2, 5]</span>
<span class="p">[</span><span class="s2">"Ava Davidson"</span><span class="p">,</span> <span class="s2">"Benjamin Anderson"</span><span class="p">,</span> <span class="s2">"Charlie Baker"</span><span class="p">]</span>
<span class="p">.</span><span class="nf">sort_by</span><span class="p">.</span><span class="nf">return_eval</span><span class="p">{</span><span class="n">_1</span><span class="p">.</span><span class="nf">split</span><span class="p">.</span><span class="nf">reverse</span><span class="p">.</span><span class="nf">join</span><span class="p">(</span><span class="s2">", "</span><span class="p">)}</span> <span class="c1"># => ["Anderson, Benjamin", "Baker, Charlie", "Davidson, Ava"]</span>
</code></pre> Ruby master - Feature #18331 (Open): Kernel.#Timehttps://bugs.ruby-lang.org/issues/183312021-11-13T12:27:30Zsawa (Tsuyoshi Sawada)
<p>I remember that, once, Matz suggested a new literal notation for date/time, but he later withdrew it. It seems like introducing new syntax at this point is less realistic. But I believe that many people have wanted a simple way to create a date/time object.</p>
<p>I propose <code>Kernel.Time</code> or <code>Kernel#Time</code>, in much of the same way as <code>Kernel.#Integer</code>, <code>Kernel.#Float</code>, <code>Kernel.#Complex</code> and others. It should take a string as the first required argument and some optional keyword arguments.</p>
<pre><code class="rb syntaxhl" data-language="rb"><span class="no">Time</span><span class="p">(</span><span class="s2">"2021-11-13T21:21:18.027294 +0900"</span><span class="p">)</span> <span class="c1"># => 2021-11-13 21:21:18.027294 +0900</span>
<span class="no">Time</span><span class="p">(</span><span class="s2">"2021-11-13 21:21:18.027294 +0900"</span><span class="p">)</span> <span class="c1"># => 2021-11-13 21:21:18.027294 +0900</span>
<span class="no">Time</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span> <span class="c1"># => nil</span>
</code></pre>
<p>I leave out the specifics. There should be room for debate.</p>
<p>I wish the features nobu implemented in <a href="https://bugs.ruby-lang.org/issues/18033" class="external">https://bugs.ruby-lang.org/issues/18033</a> for <code>Time.new</code> can be carried over to <code>Kernel.#Time</code>.</p> Ruby master - Feature #17773 (Open): Alias `Numeric#zero?` and `Float#zero?` as `Numeric#empty?` ...https://bugs.ruby-lang.org/issues/177732021-04-02T03:49:45Zsawa (Tsuyoshi Sawada)
<p>When dealing with user input fields as in web applications, there are typical values that we want to consider as the default and/or absence of user input. For string/text inputs, list items, and attributes, we have <code>String#empty?</code>, <code>Array#empty?</code>, and <code>Hash#empty?</code> respectively, which seem to correspond to those cases. As for numerics, there are <code>Numeric#zero?</code> and <code>Float#zero?</code>.</p>
<p>However, there is no single term that covers all these cases. In a routine to check through the fields whether there is user input, we have to selectively use <code>empty?</code> or <code>zero?</code> depending on the type of the input field.</p>
<p>Many programming languages other than Ruby typically consider these values as falsy with respect to logical calculation. Ruby handles only <code>nil</code> and <code>false</code> as falsy, and that has clear advantages in many aspects, but with the cost of losing a simple way to handle these default values.</p>
<p>I propose to alias <code>Numeric#zero?</code> as <code>Numeric#empty?</code> and <code>Float#zero?</code> as <code>Float#empty?</code> so that we can simply use <code>empty?</code>. At first, calling zero as empty might sound strange, but at least for non-negative integers, set theoretic definitions usually define zero as the empty set, so it is not that strange after all.</p>
<p>Ruby on Rails' <code>blank?</code> is conceptually similar to this, but <code>0.blank?</code> returns <code>false</code>, so it is a different concept.</p> Ruby master - Feature #17608 (Rejected): Compact and sum in one stephttps://bugs.ruby-lang.org/issues/176082021-02-04T06:18:38Zsawa (Tsuyoshi Sawada)
<p>Many use cases of <code>Array#sum</code> are preceded with the <code>compact</code> method or are followed by a block to ensure the value is addable.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">sum</span> <span class="c1"># !> TypeError</span>
<span class="n">a</span><span class="p">.</span><span class="nf">compact</span><span class="p">.</span><span class="nf">sum</span> <span class="c1"># => 6</span>
<span class="n">a</span><span class="p">.</span><span class="nf">sum</span><span class="p">{</span><span class="n">_1</span> <span class="o">||</span> <span class="mi">0</span><span class="p">}</span> <span class="c1"># => 6</span>
</code></pre>
<p>I propose there should be a way to do that in one step. I request either of the following:</p>
<p>A. Change the current behaviour to skip <code>nil</code>s.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">sum</span> <span class="c1"># => 6</span>
</code></pre>
<p>B. <code>Array#filter_sum</code> method</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">filter_sum</span> <span class="c1"># => 6</span>
</code></pre>
<p>C. An option for <code>Array#sum</code></p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">sum</span><span class="p">(</span><span class="ss">compact: </span><span class="kp">true</span><span class="p">)</span> <span class="c1"># => 6</span>
</code></pre> Ruby master - Feature #17177 (Rejected): Include the current file name and the line number in the...https://bugs.ruby-lang.org/issues/171772020-09-18T12:17:57Zsawa (Tsuyoshi Sawada)
<p>In many debugging situations, we want to inspect some objects in more than one source location throughout the code. To be identified where it is called from, it is very common that the method <code>p</code> is used together with a <code>puts</code> method that outputs some marker that distinguishes the location:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="o">...</span> <span class="c1"># some buggy area or conditional branch</span>
<span class="nb">puts</span> <span class="s2">"== A =="</span>
<span class="nb">p</span> <span class="n">foo</span>
<span class="o">...</span> <span class="c1"># another buggy area or conditional branch</span>
<span class="nb">puts</span> <span class="s2">"== B =="</span>
<span class="nb">p</span> <span class="n">foo</span>
<span class="o">...</span> <span class="c1"># another buggy area or conditional branch</span>
<span class="nb">puts</span> <span class="s2">"bar is:"</span>
<span class="nb">p</span> <span class="n">bar</span>
<span class="o">...</span>
</code></pre>
<p>But this is cumbersome.</p>
<p>Also, after debugging, when we want to remove the <code>p</code> calls from the code, it is sometimes not so trivial to identify where those method calls are written.</p>
<p>I propose that the method <code>p</code> should display not only the objects passed as arguments, but also its source location. Supposing we have a file <code>bar.rb</code> like this,</p>
<pre><code class="rb syntaxhl" data-language="rb"><span class="n">foo</span> <span class="o">=</span> <span class="s2">"a"</span>
<span class="nb">p</span> <span class="n">foo</span>
</code></pre>
<p>running <code>ruby bar.rb</code> should perhaps have an output like this:</p>
<pre><code>At bar.rb:2
"a"
</code></pre>
<p>Then, in a debugging situation like the above, we would only need to write:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="o">...</span> <span class="c1"># some buggy area or conditional branch</span>
<span class="nb">p</span> <span class="n">foo</span>
<span class="o">...</span> <span class="c1"># another buggy area or conditional branch</span>
<span class="nb">p</span> <span class="n">foo</span>
<span class="o">...</span> <span class="c1"># another buggy area or conditional branch</span>
<span class="nb">p</span> <span class="n">bar</span>
<span class="o">...</span>
</code></pre> Ruby master - Feature #17165 (Open): Add `filter` and `flatten` keywords to `Enumerable#map`https://bugs.ruby-lang.org/issues/171652020-09-12T14:46:54Zsawa (Tsuyoshi Sawada)
<p>I had a use case to do <code>map</code> on an enumerable, with 1-level flattening, while skipping <code>nil</code> values.</p>
<p>There are convenient <code>Enumerable#flat_map</code> and <code>Enumerable#filter_map</code> methods, but the problem is that they cannot be used at the same time. I had to chose to do either of the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">array</span>
<span class="p">.</span><span class="nf">filter_map</span> <span class="k">do</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
<span class="n">bar</span> <span class="o">=</span> <span class="n">baz</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="k">next</span> <span class="k">unless</span> <span class="n">bar</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">...</span><span class="p">}</span>
<span class="k">end</span>
<span class="p">.</span><span class="nf">flatten</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">array</span>
<span class="p">.</span><span class="nf">flat_map</span> <span class="k">do</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
<span class="n">bar</span> <span class="o">=</span> <span class="n">baz</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="k">next</span> <span class="k">unless</span> <span class="n">bar</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">...</span><span class="p">}</span>
<span class="k">end</span>
<span class="p">.</span><span class="nf">compact</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">array</span>
<span class="p">.</span><span class="nf">flat_map</span> <span class="k">do</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
<span class="n">bar</span> <span class="o">=</span> <span class="n">baz</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="k">next</span> <span class="p">[]</span> <span class="k">unless</span> <span class="n">bar</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">...</span><span class="p">}</span>
<span class="k">end</span>
</code></pre>
<p>The last one of the above may not look so bad, but it requires an extra consideration, and is a bit hacky. When you are in a hurry, it just might not come to your mind.</p>
<p>This led me to realize that <code>flat_map</code> and <code>filter_map</code> should not be independent operations, but are rather some different modes of the operation <code>map</code>. There is no reason for the modes to be mutually exclusive of one another, and a use case that I mentioned above may arise.</p>
<p>I propose to add <code>filter</code> and <code>flatten</code> as optional keyword arguments to <code>Enumerable#map</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">array</span>
<span class="p">.</span><span class="nf">map</span><span class="p">(</span><span class="ss">filter: </span><span class="kp">true</span><span class="p">,</span> <span class="ss">flatten: </span><span class="mi">1</span><span class="p">)</span> <span class="k">do</span> <span class="o">|</span><span class="n">foo</span><span class="o">|</span>
<span class="n">bar</span> <span class="o">=</span> <span class="n">baz</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span>
<span class="k">next</span> <span class="k">unless</span> <span class="n">bar</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">...</span><span class="p">}</span>
<span class="k">end</span>
</code></pre>
<p>In fact, even when the two parameters are not used together, I believe it would be easier to the brain and I would feel much more comfortable to pass <code>filter: true</code> or <code>flatten: 1</code> to <code>map</code> when necessary rather than having to deicide whether to use <code>map</code> or <code>flat_map</code> or use <code>map</code> or <code>filter_map</code>.</p>
<p>Furthermore, this would make it possible to do flattening of an arbitrary depth (as specified by the parameter) during map.</p> Ruby master - Feature #17006 (Open): Let `Kernel#Hash` take a block to provide the default valuehttps://bugs.ruby-lang.org/issues/170062020-07-03T08:09:01Zsawa (Tsuyoshi Sawada)
<p>Sometimes, we need to create a hash that has explicit key-value pairs as well as a default value, but there has not been a way to do that at once. The most naive way is to do like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">}</span>
<span class="n">h</span><span class="p">.</span><span class="nf">default_proc</span> <span class="o">=</span> <span class="o">-></span><span class="p">(</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="p">){</span><span class="k">raise</span> <span class="s2">"Unknown key </span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">"</span><span class="p">}</span>
<span class="n">h</span> <span class="c1"># => ...</span>
</code></pre>
<p>A more sophisticated way is this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Hash</span><span class="p">.</span><span class="nf">new</span><span class="p">{</span><span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="k">raise</span> <span class="s2">"Unknown key </span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">"</span><span class="p">}.</span><span class="nf">merge</span><span class="p">({</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">})</span>
</code></pre>
<p>But that is still not short enough, and it also looks backwards, i.e., we usually want to specify the explicit key-value pairs before the default value.</p>
<p>My proposal is to allow <code>Kernel#Hash</code> to take a block that provides the default value in the same way as the block of <code>Hash.new</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Hash</span><span class="p">({</span><span class="ss">a: </span><span class="mi">1</span><span class="p">,</span> <span class="ss">b: </span><span class="mi">2</span><span class="p">}){</span><span class="o">|</span><span class="n">h</span><span class="p">,</span> <span class="n">k</span><span class="o">|</span> <span class="k">raise</span> <span class="s2">"Unknown key </span><span class="si">#{</span><span class="n">k</span><span class="si">}</span><span class="s2">"</span><span class="p">}</span>
</code></pre> Ruby master - Bug #16842 (Closed): `inspect` prints the UTF-8 character U+0085 (NEXT LINE) verbat...https://bugs.ruby-lang.org/issues/168422020-05-09T14:35:11Zsawa (Tsuyoshi Sawada)
<p>The UTF-8 character U+0085 (NEXT LINE) is not printable, but <code>inspect</code> prints the character verbatim (within double quotation):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mh">0x85</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">).</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="mh">0x85</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">).</span><span class="nf">inspect</span>
<span class="c1">#=> "\"</span>
<span class="p">\</span><span class="s2">""</span>
</code></pre>
<p>My understanding is that non-printable characters are not printed verbatim with <code>inspect</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1">#=> "\"\\n\""</span>
</code></pre>
<p>while printable characters are:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"a"</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="s2">"a"</span><span class="p">.</span><span class="nf">inspect</span> <span class="c1"># => "\"a\""</span>
</code></pre>
<p>I ran the following script, and found that U+0085 is the only character within the range U+0000 to U+FFFF that behaves like this.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">verbatim?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="o">!</span><span class="n">char</span><span class="p">.</span><span class="nf">inspect</span><span class="p">.</span><span class="nf">start_with?</span><span class="p">(</span><span class="sr">%r{</span><span class="se">\"\\</span><span class="sr">[a-z]}</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">def</span> <span class="nf">printable?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="n">char</span><span class="p">.</span><span class="nf">match?</span><span class="p">(</span><span class="sr">/\p{print}/</span><span class="p">)</span>
<span class="k">end</span>
<span class="p">(</span><span class="mh">0x0000</span><span class="o">..</span><span class="mh">0xffff</span><span class="p">).</span><span class="nf">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">i</span><span class="o">|</span>
<span class="k">begin</span>
<span class="n">char</span> <span class="o">=</span> <span class="n">i</span><span class="p">.</span><span class="nf">chr</span><span class="p">(</span><span class="no">Encoding</span><span class="o">::</span><span class="no">UTF_8</span><span class="p">)</span>
<span class="k">rescue</span> <span class="no">RangeError</span>
<span class="k">next</span>
<span class="k">end</span>
<span class="nb">puts</span> <span class="s1">'%#x'</span> <span class="o">%</span> <span class="n">i</span> <span class="k">unless</span> <span class="n">verbatim?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span> <span class="o">==</span> <span class="n">printable?</span><span class="p">(</span><span class="n">char</span><span class="p">)</span>
<span class="k">end</span>
</code></pre> Ruby master - Bug #16685 (Closed): IRB auto indent does not work for single-line method definitionshttps://bugs.ruby-lang.org/issues/166852020-03-10T21:41:44Zsawa (Tsuyoshi Sawada)
<p>IRB auto indent does not work correctly for single line method definitions within a class. It ends up like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">001</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">002</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">def</span> <span class="nf">a</span><span class="p">;</span> <span class="k">end</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">003</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">def</span> <span class="nf">b</span><span class="p">;</span> <span class="k">end</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">004</span><span class="p">:</span><span class="mi">0</span><span class="o">></span> <span class="k">end</span>
<span class="o">=></span> <span class="ss">:b</span>
</code></pre>
<p>I believe it should end up like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">001</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">class</span> <span class="nc">Foo</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">002</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">def</span> <span class="nf">a</span><span class="p">;</span> <span class="k">end</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">003</span><span class="p">:</span><span class="mi">1</span><span class="o">*</span> <span class="k">def</span> <span class="nf">b</span><span class="p">;</span> <span class="k">end</span>
<span class="n">irb</span><span class="p">(</span><span class="n">main</span><span class="p">):</span><span class="mo">004</span><span class="p">:</span><span class="mi">0</span><span class="o">></span> <span class="k">end</span>
<span class="o">=></span> <span class="ss">:b</span>
</code></pre> Ruby master - Bug #16599 (Closed): did_you_mean is not activated for NameError and KeyErrorhttps://bugs.ruby-lang.org/issues/165992020-01-28T20:26:23Zsawa (Tsuyoshi Sawada)
<p><a href="https://www.rubydoc.info/gems/did_you_mean/" class="external">This document</a> claims that the did_you_mean gem responds to NameError, NoMethodError, and KeyError, but it actually seems to only respond to NoMethodError.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span><span class="p">()</span>
<span class="c1"># >> NoMethodError: undefined method `foo' for main:Object</span>
<span class="c1"># >> Did you mean? for</span>
<span class="n">foo</span>
<span class="c1"># >> NameError: undefined local variable or method `foo' for main:Object</span>
<span class="no">Foo</span>
<span class="c1"># >> NameError: uninitialized constant Foo</span>
<span class="p">{</span><span class="ss">foo: </span><span class="mi">1</span><span class="p">}.</span><span class="nf">fetch</span><span class="p">(</span><span class="ss">:bar</span><span class="p">)</span>
<span class="c1"># >> KeyError: key not found: :bar</span>
</code></pre> Ruby master - Bug #16506 (Closed): Documentation for `Module#const_souce_location` is wronghttps://bugs.ruby-lang.org/issues/165062020-01-12T18:17:28Zsawa (Tsuyoshi Sawada)
<p><a href="https://ruby-doc.org/core-2.7.0/Module.html#method-i-const_source_location" class="external">https://ruby-doc.org/core-2.7.0/Module.html#method-i-const_source_location</a> says:</p>
<blockquote>
<p>Returns the Ruby source filename and line number containing <strong>first</strong> definition of constant specified.</p>
</blockquote>
<p>It should be:</p>
<blockquote>
<p>Returns the Ruby source filename and line number containing the <strong>last</strong> definition (the effective definition) of the constant specified.</p>
</blockquote>
<p>It also has an example line:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">p</span> <span class="no">Object</span><span class="p">.</span><span class="nf">const_source_location</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span> <span class="c1"># => ["test.rb", 1] -- note it is first entry, not "continuation"</span>
</code></pre>
<p>but that may give the impression that the first-ness is due to the nature of this method. The reason <code>["test.rb", 1]</code> is returned instead of <code>["test.rb", 14]</code> is because the constant is created at line 1, and is only reopened/modified in line 14. Perhaps, this line can be changed to:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">p</span> <span class="no">Object</span><span class="p">.</span><span class="nf">const_source_location</span><span class="p">(</span><span class="s1">'A'</span><span class="p">)</span> <span class="c1"># => ["test.rb", 1] -- note 'A' is created in line 1, and is only reopened/modified in "continuation"</span>
</code></pre>
<p>Adding something like this may make it clear that it is the last definition that is returned:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">D</span> <span class="o">=</span> <span class="s1">'D1'</span>
<span class="no">D</span> <span class="o">=</span> <span class="s1">'D2'</span>
<span class="no">D</span> <span class="o">=</span> <span class="s1">'D3'</span>
<span class="o">...</span>
<span class="no">Object</span><span class="p">.</span><span class="nf">const_source_location</span><span class="p">(</span><span class="s1">'D'</span><span class="p">)</span> <span class="c1"># => returns the location that corresponds to 'D3'</span>
</code></pre> Ruby master - Feature #16446 (Rejected): Enumerable#take_*, Enumerable#drop_* counterparts with p...https://bugs.ruby-lang.org/issues/164462019-12-23T08:26:05Zsawa (Tsuyoshi Sawada)
<p><a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Enumerable#take_while_after (Rejected)" href="https://bugs.ruby-lang.org/issues/16441">#16441</a> led me to think about the issue more generally. When we want to split a series of iterations by the first element that satisfies (or dissatisfies) a condition, we have three factors to consider.</p>
<p>(1) Whether we want the condition to work <strong>negatively</strong> or <strong>positively</strong><br>
(2) Whether we want the first element to satisfy (or dissatisfy) the condition to be included in the <strong>left</strong> side or the <strong>right</strong> side of the split<br>
(3) Whether we want the <strong>left</strong> side or the <strong>right</strong> side in the returned output</p>
<p>This leads us to eight possible combinations to consider.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">enum</span> <span class="o">=</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">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">5</span><span class="p">].</span><span class="nf">to_enum</span>
</code></pre>
<table>
<thead>
<tr>
<th></th>
<th>(1)</th>
<th>(2)</th>
<th>(3)</th>
<th>method</th>
<th>example</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>negatively</td>
<td>left</td>
<td>left</td>
<td><code>take_while</code></td>
<td><code>enum.foo1(&:nonzero?) # => [1, 1]</code></td>
</tr>
<tr>
<td>2</td>
<td>negatively</td>
<td>left</td>
<td>right</td>
<td><code>drop_while</code></td>
<td><code>enum.foo2(&:nonzero?) # => [0, 3, 3, 0, 5, 5]</code></td>
</tr>
<tr>
<td>3</td>
<td>negatively</td>
<td>right</td>
<td>left</td>
<td></td>
<td><code>enum.foo3(&:nonzero?) # => [1, 1, 0]</code></td>
</tr>
<tr>
<td>4</td>
<td>negatively</td>
<td>right</td>
<td>right</td>
<td></td>
<td><code>enum.foo4(&:nonzero?) # => [3, 3, 0, 5, 5]</code></td>
</tr>
<tr>
<td>5</td>
<td>positively</td>
<td>left</td>
<td>left</td>
<td></td>
<td><code>enum.foo5(&:zero?) # => [1, 1]</code></td>
</tr>
<tr>
<td>6</td>
<td>positively</td>
<td>left</td>
<td>right</td>
<td></td>
<td><code>enum.foo6(&:zero?) # => [0, 3, 3, 0, 5, 5]</code></td>
</tr>
<tr>
<td>7</td>
<td>positively</td>
<td>right</td>
<td>left</td>
<td></td>
<td><code>enum.foo7(&:zero?) # => [1, 1, 0]</code></td>
</tr>
<tr>
<td>8</td>
<td>positively</td>
<td>right</td>
<td>right</td>
<td></td>
<td><code>enum.foo8(&:zero?) # => [3, 3, 0, 5, 5]</code></td>
</tr>
</tbody>
</table>
<p>Proposal <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Enumerable#take_while_after (Rejected)" href="https://bugs.ruby-lang.org/issues/16441">#16441</a> asks for a method that corresponds to case 3 in the table above, but I think that would make the paradigm messy unless case 4 is also implemented. Either cases 3 and 4 should both be implemented, or both not. Actually, the current proposal is not about cases 3 and 4. I would leave that to <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Backport request: MJIT crashes on a certain flow of branches (Closed)" href="https://bugs.ruby-lang.org/issues/16641">#16641</a>.</p>
<p>In many use cases (including the first example in <a class="issue tracker-1 status-5 priority-4 priority-default closed" title="Bug: Backport request: MJIT crashes on a certain flow of branches (Closed)" href="https://bugs.ruby-lang.org/issues/16641">#16641</a>), we want to detect the "marker element" by which we split the iterations. In the cases above, that can be the element <code>0</code>. In such use cases, it is more natural to describe the condition in positive terms (i.e., <code>zero?</code>) rather than negative terms (i.e., <code>nonzero?</code>). (And in other use cases, it might be the other way around.) So I would like to propose methods that correspond to cases 5, 6, 7, 8 above.</p>
<p>Naming of the methods should be done systematically. As a candidate, I came up with the following:</p>
<table>
<thead>
<tr>
<th></th>
<th>method</th>
</tr>
</thead>
<tbody>
<tr>
<td>5</td>
<td><code>take_before</code></td>
</tr>
<tr>
<td>6</td>
<td><code>drop_before</code></td>
</tr>
<tr>
<td>7</td>
<td><code>take_upto</code></td>
</tr>
<tr>
<td>8</td>
<td><code>drop_upto</code></td>
</tr>
</tbody>
</table> Ruby master - Bug #16050 (Rejected): :@ is not parsed correctlyhttps://bugs.ruby-lang.org/issues/160502019-08-07T10:56:02Zsawa (Tsuyoshi Sawada)
<p>Symbol literal allows <code>@</code> as the first character:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="ss">:@foo</span> <span class="c1"># => :@foo</span>
</code></pre>
<p>Nevertheless, it does not allow <code>@</code> by itself:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">:</span><span class="err">@</span> <span class="c1"># >> SyntaxError: `@' without identifiers is not allowed as an instance variable name</span>
</code></pre>
<p>This resembles <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: :~@ and :!@ are not parsed correctly (Rejected)" href="https://bugs.ruby-lang.org/issues/10463">#10463</a>, but while <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: :~@ and :!@ are not parsed correctly (Rejected)" href="https://bugs.ruby-lang.org/issues/10463">#10463</a> is related to placeholder in a method name, the issue here is related to instance variable name, so I think they are different issues.</p> Ruby master - Feature #16037 (Open): Allow multiple single/double-splatted variables in `in` patt...https://bugs.ruby-lang.org/issues/160372019-08-02T11:50:30Zsawa (Tsuyoshi Sawada)
<p>Pattern matching in <code>in</code> argument seems to prohibit multiple occurrences of single/double-splatted variables.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</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="mi">3</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="k">in</span> <span class="o">*</span><span class="n">foo</span><span class="p">,</span> <span class="no">Integer</span><span class="p">,</span> <span class="n">bar</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># >> (Not SyntaxError)</span>
<span class="k">case</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="mi">3</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="k">in</span> <span class="o">*</span><span class="n">foo</span><span class="p">,</span> <span class="no">Integer</span><span class="p">,</span> <span class="o">*</span><span class="n">bar</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># >> SyntaxError: unexpected *</span>
</code></pre>
<p>However, unlike conventional constant/variable assignment, it makes sense to have multiple occurrences of single/double-splatted variables in a single pattern matching provided that we have a definite rule regarding whether the splats are greedy or not.</p>
<p>I propose the following.</p>
<ol>
<li>Relax the syntax for pattern matching in <code>in</code> argument to allow multiple occurrences of single/double-splatted variables, and set up a rule regarding whether the splats are greedy or not; preferably greedy.</li>
<li>Further, introduce new syntax for non-greedy splats <code>*?foo</code>, <code>**?foo</code>. Currently, they are syntactically invalid, so I don't think they would conflict with existing code.</li>
</ol>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</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="mi">3</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="mi">6</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">,</span> <span class="s2">"f"</span><span class="p">];</span> <span class="k">in</span> <span class="o">*</span><span class="n">foo</span><span class="p">,</span> <span class="no">Integer</span><span class="p">,</span> <span class="o">*</span><span class="n">bar</span><span class="p">;</span> <span class="n">foo</span> <span class="k">end</span> <span class="c1"># => ["a", "b", 3, "c", "d"]</span>
<span class="k">case</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="mi">3</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="mi">6</span><span class="p">,</span> <span class="s2">"e"</span><span class="p">,</span> <span class="s2">"f"</span><span class="p">];</span> <span class="k">in</span> <span class="o">*</span><span class="p">?</span><span class="n">foo</span><span class="p">,</span> <span class="no">Integer</span><span class="p">,</span> <span class="o">*</span><span class="n">bar</span><span class="p">;</span> <span class="n">foo</span> <span class="k">end</span> <span class="c1"># => ["a", "b"]</span>
</code></pre> Ruby master - Bug #15987 (Closed): Let `exception` option in `Kernel#Complex`, `Kernel#Float`, `K...https://bugs.ruby-lang.org/issues/159872019-07-05T03:12:16Zsawa (Tsuyoshi Sawada)
<p>The <code>exception</code> option in <code>Kernel#Complex</code>, <code>Kernel#Float</code>, <code>Kernel#Integer</code>, and <code>Kernel#Rational</code> distinguishes <code>false</code> vs. other values.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Integer</span><span class="p">(</span><span class="s2">"z"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span> <span class="c1">#=> nil</span>
<span class="no">Integer</span><span class="p">(</span><span class="s2">"z"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">nil</span><span class="p">)</span> <span class="c1">#>> ArgumentError: invalid value for Integer(): "z")</span>
</code></pre>
<p>But in most other cases where a boolean notion is concerned (for example, the <code>chomp</code> option in <code>Kernel#gets</code>), the distinction is between falsy vs. truthy values.</p>
<p>I request the distinction to be falsy vs. truthy. In other words, I would like the value <code>nil</code> to work on the falsy side rather than the truthy side like this.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Integer</span><span class="p">(</span><span class="s2">"z"</span><span class="p">,</span> <span class="ss">exception: </span><span class="kp">false</span><span class="p">)</span> <span class="c1">#=> nil</span>
<span class="no">Integer</span><span class="p">(</span><span class="s2">"z"</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 #15950 (Rejected): Allow negative length in `Array#[]`, `Array#[]=`, `Array...https://bugs.ruby-lang.org/issues/159502019-06-22T03:09:43Zsawa (Tsuyoshi Sawada)
<p>To take the first n characters of a string, using <code>[]</code> is straightforward:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcdefgh"</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="c1"># => "abc"</span>
</code></pre>
<p>But to take the last n characters, we need to use n in two arguments: in the index (in negative form) in addition to the length:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcdefgh"</span><span class="p">[</span><span class="o">-</span><span class="mi">3</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span> <span class="c1"># => "fgh"</span>
</code></pre>
<p>This is cumbersome.</p>
<p>I wish negative length to be allowed, and be interpreted as measuring leftward (while cycling the receiver if necessary).</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcdefgh"</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="c1"># => "fgh"</span>
<span class="s2">"abcdefgh"</span><span class="p">[</span><span class="mi">5</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="c1"># => "cde"</span>
</code></pre>
<p>If there is not enough characters or elements, it should stop at the boundary.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="s2">"abcdefgh"</span><span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">]</span> <span class="c1"># => "a"</span>
</code></pre> Ruby master - Feature #15722 (Feedback): `Kernel#case?`https://bugs.ruby-lang.org/issues/157222019-03-22T07:12:32Zsawa (Tsuyoshi Sawada)
<p>I often want to use <code>===</code> to match a single object on the right side against multiple objects on the left, as is used in <code>case</code>-constructions, just to return a truth value, and end up writing like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">bar</span> <span class="c1"># => "bar"</span>
<span class="n">flag1</span> <span class="o">=</span> <span class="k">case</span> <span class="n">bar</span><span class="p">;</span> <span class="k">when</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="kp">true</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># => true</span>
<span class="n">flag2</span> <span class="o">=</span> <span class="k">case</span> <span class="n">bar</span><span class="p">;</span> <span class="k">when</span> <span class="no">Symbol</span><span class="p">,</span> <span class="no">String</span><span class="p">;</span> <span class="kp">true</span><span class="p">;</span> <span class="k">end</span> <span class="c1"># => true</span>
</code></pre>
<p>I propose <code>Kernel#case?</code> that should work like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">bar</span> <span class="c1"># => "bar"</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">case?</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="c1"># => true</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">case?</span><span class="p">(</span><span class="s2">"qux"</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">case?</span><span class="p">(</span><span class="no">Symbol</span><span class="p">,</span> <span class="no">String</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">case?</span><span class="p">(</span><span class="no">Array</span><span class="p">)</span> <span class="c1"># => false</span>
<span class="n">bar</span><span class="p">.</span><span class="nf">case?</span> <span class="c1"># => false</span>
</code></pre>
<p>It is similar to Rails' <code>in?</code>, but it differs from it in that it uses <code>===</code> for comparison, not <code>==</code>.</p>
<p>Or, alternatively, allowing <code>Kernel#instance_of?</code> and <code>Kernel#kind_of?</code> to allow multiple arguments may be a compromise.</p> Ruby master - Bug #15708 (Rejected): Implicit numbered argument decomposes an arrayhttps://bugs.ruby-lang.org/issues/157082019-03-19T11:08:39Zsawa (Tsuyoshi Sawada)
<p>In the following, <code>@1</code> refers to the entire item iterated:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="err">@</span><span class="mi">1</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
</code></pre>
<p>whereas in the following, <code>@1</code> refers to the first item achieved by decomposing the item iterated, behaving the same as <code>x</code> given by <code>|(x)|</code> rather than by <code>|x|</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[[</span><span class="mi">1</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">]]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [[1], [2], [3]]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="p">(</span><span class="n">x</span><span class="p">)</span><span class="o">|</span> <span class="n">x</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
<span class="n">a</span><span class="p">.</span><span class="nf">map</span><span class="p">{</span><span class="err">@</span><span class="mi">1</span><span class="p">}</span> <span class="c1"># => [1, 2, 3]</span>
</code></pre>
<p>Is this intended?</p> Ruby master - Misc #15654 (Open): Documentation for Complex is wrong or misleadinghttps://bugs.ruby-lang.org/issues/156542019-03-11T08:43:36Zsawa (Tsuyoshi Sawada)
<p>The documentation for <code>Complex</code> <a href="https://ruby-doc.org/core-2.6/Complex.html" class="external">https://ruby-doc.org/core-2.6/Complex.html</a> says or implies that a complex can be created by literal like <code>2+1i</code>, but that is actually calling the method <code>+</code> on receiver <code>2</code> with argument <code>1i</code>. The description should be changed to make it clear that <code>2+ 1i</code> is not a literal but is applying a method.</p> Ruby master - Feature #15627 (Open): Appearance of custom singleton classeshttps://bugs.ruby-lang.org/issues/156272019-02-28T11:47:27Zsawa (Tsuyoshi Sawada)
<p>When I have a singleton class <code>AClass</code> of an instance <code>a</code> of a custom class <code>A</code>,</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">end</span>
<span class="n">a</span> <span class="o">=</span> <span class="no">A</span><span class="p">.</span><span class="nf">new</span>
<span class="no">AClass</span> <span class="o">=</span> <span class="n">a</span><span class="p">.</span><span class="nf">singleton_class</span>
</code></pre>
<p>i) even though the singleton class of <code>nil</code>, <code>false</code>, and <code>true</code> are referred to by their assigned constant names, the singleton class <code>AClass</code> of <code>a</code> is not:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">nil</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="c1">#=> NilClass</span>
<span class="kp">false</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="c1">#=> FalseClass</span>
<span class="kp">true</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="c1">#=> TrueClass</span>
<span class="n">a</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="c1">#=> #<Class:#<A:0x00007fda832a7eb0>></span>
</code></pre>
<p>ii) even though the singleton class of <code>nil</code>, <code>false</code>, and <code>true</code> appear as their class, the singleton class <code>AClass</code> of <code>a</code> does not:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="kp">nil</span><span class="p">.</span><span class="nf">class</span> <span class="c1">#=> NilClass</span>
<span class="kp">false</span><span class="p">.</span><span class="nf">class</span> <span class="c1">#=> FalseClass</span>
<span class="kp">true</span><span class="p">.</span><span class="nf">class</span> <span class="c1">#=> TrueClass</span>
<span class="n">a</span><span class="p">.</span><span class="nf">class</span> <span class="c1">#=> A</span>
</code></pre>
<p>This contrast between <code>nil</code>, <code>false</code>, and <code>true</code> on the one hand and <code>a</code> on the other is confusing. I am actually not sure if this is intended behaviour It may be related to</p>
<ul>
<li><a href="https://bugs.ruby-lang.org/issues/15608" class="external">https://bugs.ruby-lang.org/issues/15608</a></li>
<li><a href="https://bugs.ruby-lang.org/issues/14895" class="external">https://bugs.ruby-lang.org/issues/14895</a></li>
</ul>
<p>I expect <code>AClass</code> to behave the same as with <code>NilClass</code>, <code>FalseClass</code>, and <code>TrueClass</code>. I expect:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span><span class="p">.</span><span class="nf">singleton_class</span> <span class="c1">#=> AClass</span>
<span class="n">a</span><span class="p">.</span><span class="nf">class</span> <span class="c1">#=> AClass</span>
</code></pre>
<p>If the current behaviour is intended, I would like this to become a feature request.</p> Ruby master - Feature #15612 (Feedback): A construct to restrict the scope of local variableshttps://bugs.ruby-lang.org/issues/156122019-02-19T12:42:21Zsawa (Tsuyoshi Sawada)
<p>We sometimes have local variables that are to be used only to keep track of some temporal states/values during a short routine:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="o">...</span>
<span class="n">foo</span> <span class="o">=</span> <span class="n">some_initial_value</span>
<span class="n">some_routine_that_uses_foo</span>
<span class="o">...</span>
</code></pre>
<p>Currently, the scope of local variables are either a proc, a block, <code>loop</code> body, a method definition, or a class/module definition, but such routines are sometimes just only a part of them.</p>
<p>In order to improve readability of the code by explicitly indicating the scope of such local variables, and to avoid pollution by the variable, I propose to have some construct to restrict the scope of local variables.</p>
<p>One possibility, without adding a new keyword to the current syntax, is to use the <code>begin</code>...<code>end</code> construct. The expected behavior would be:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">begin</span>
<span class="n">foo</span> <span class="o">=</span> <span class="s2">"foo"</span>
<span class="n">foo</span> <span class="c1"># => "foo"</span>
<span class="k">end</span>
<span class="n">foo</span> <span class="c1"># => `nil`, or "Undefined local variable or method error"</span>
</code></pre>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">foo</span> <span class="o">=</span> <span class="s2">"bar"</span>
<span class="k">begin</span>
<span class="n">foo</span> <span class="o">=</span> <span class="s2">"foo"</span>
<span class="n">foo</span> <span class="c1"># => "foo"</span>
<span class="k">end</span>
<span class="n">foo</span> <span class="c1"># => "bar"</span>
</code></pre>
<p>Or, does this break the existing code too much? If so, can a new construct be added to the current syntax?</p> 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 #15381 (Open): Let double splat call `to_h` implicitlyhttps://bugs.ruby-lang.org/issues/153812018-12-05T08:25:04Zsawa (Tsuyoshi Sawada)
<p>The single splat calls <code>to_a</code> implicitly on the object (if it is not an array already) so that, for example, we have the convenience of writing conditions in an array literal:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[</span>
<span class="o">*</span><span class="p">(</span><span class="ss">:foo</span> <span class="k">if</span> <span class="n">some_condition</span><span class="p">),</span>
<span class="o">*</span><span class="p">(</span><span class="ss">:bar</span> <span class="k">if</span> <span class="n">another_condition</span><span class="p">),</span>
<span class="p">]</span>
</code></pre>
<p>And the ampersand implicitly calls <code>to_proc</code> on the object (if it is not a proc already) so that we can substitute a block with an ampersand followed by a symbol:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">some_method</span><span class="p">(</span><span class="o">&</span><span class="ss">:some_method_name</span><span class="p">)</span>
</code></pre>
<p>Unlike the single splat and ampersand, the double splat does not seem to implicitly call a corresponding method. I propose that the double splat should call <code>to_h</code> implicitly on the object if it not already a Hash so that we can, for example, write a condition in a hash literal as follows:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{</span>
<span class="o">**</span><span class="p">({</span><span class="ss">a: </span><span class="mi">1</span><span class="p">}</span> <span class="k">if</span> <span class="n">some_condition</span><span class="p">),</span>
<span class="o">**</span><span class="p">({</span><span class="ss">b: </span><span class="mi">2</span><span class="p">)</span> <span class="k">if</span> <span class="n">another_condition</span><span class="p">),</span>
<span class="p">}</span>
</code></pre>
<p>There may be some other benefits of this feature that I have not noticed yet.</p> Ruby master - Feature #15143 (Closed): Extend `Enumerable#to_h`https://bugs.ruby-lang.org/issues/151432018-09-20T02:23:27Zsawa (Tsuyoshi Sawada)
<p>Often, we call <code>Array#to_h</code> to the result of <code>Enumerable#map</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]}.</span><span class="nf">to_h</span>
<span class="c1">#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}</span>
</code></pre>
<p>I am thinking of a feature to do this in a single method call.</p>
<p>Currently, <code>Enumerable#to_h</code> does not accept a block. I propose that, when <code>Enumerable#to_h</code> is called with a block (that has a subarray representing a key-value pair), return a hash that would be returned by applying the block to <code>map</code>, and <code>to_h</code> to the result:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">5</span><span class="p">).</span><span class="nf">to_h</span><span class="p">{</span><span class="o">|</span><span class="n">x</span><span class="o">|</span> <span class="p">[</span><span class="n">x</span><span class="p">,</span> <span class="n">x</span> <span class="o">**</span> <span class="mi">2</span><span class="p">]}</span>
<span class="c1">#=> {1=>1, 2=>4, 3=>9, 4=>16, 5=>25}</span>
</code></pre>
<p>Ideally, I request this to be done internally to Ruby without creating an intermediate parent array.</p> Ruby master - Feature #14559 (Closed): ENV.slicehttps://bugs.ruby-lang.org/issues/145592018-02-28T08:39:22Zsawa (Tsuyoshi Sawada)
<p>I would like to request <code>ENV.slice</code>, which should behave analogous to <code>Hash#slice</code>.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">ENV</span><span class="p">.</span><span class="nf">slice</span><span class="p">(</span><span class="s2">"PATH"</span><span class="p">,</span> <span class="s2">"FOO"</span><span class="p">)</span>
<span class="c1"># => {"PATH" => "/foo:/bar", "FOO" => "foo"}</span>
</code></pre> Ruby master - Feature #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 #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 - 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 #12648 (Open): `Enumerable#sort_by` with descending optionhttps://bugs.ruby-lang.org/issues/126482016-08-02T09:57:26Zsawa (Tsuyoshi Sawada)
<p>I would like to pass an optional argument to <code>Enumerable#sort_by</code> or <code>Enumerable#sort_by!</code> to allow descending sort. When the sort key is singular, this could be done by passing a single optinal boolean variable that represents ascending when <code>false</code> (default) and descending when <code>true</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="o">&</span><span class="ss">:itself</span><span class="p">)</span> <span class="c1"># => [1, 2, 3]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">false</span><span class="p">,</span> <span class="o">&</span><span class="ss">:itself</span><span class="p">)</span> <span class="c1"># => [1, 2, 3]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">true</span><span class="p">,</span> <span class="o">&</span><span class="ss">:itself</span><span class="p">)</span> <span class="c1"># => [3, 2, 1]</span>
</code></pre>
<p>When there are multiple sort keys, corresponding numbers of arguments should be passed:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</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="n">e</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [0, 2, 1, 3]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">false</span><span class="p">,</span> <span class="kp">false</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="n">e</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [0, 2, 1, 3]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">false</span><span class="p">,</span> <span class="kp">true</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="n">e</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [2, 0, 3, 1]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">true</span><span class="p">,</span> <span class="kp">false</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="n">e</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [1, 3, 0, 2]</span>
<span class="p">[</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">].</span><span class="nf">sort_by</span><span class="p">(</span><span class="kp">true</span><span class="p">,</span> <span class="kp">true</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="p">[</span><span class="n">e</span> <span class="o">%</span> <span class="mi">2</span><span class="p">,</span> <span class="n">e</span><span class="p">]}</span> <span class="c1"># => [3, 1, 2, 0]</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 #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 #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 #12145 (Open): Aliashood between `size` and `length` is not consistenthttps://bugs.ruby-lang.org/issues/121452016-03-05T19:12:01Zsawa (Tsuyoshi Sawada)
<p>When <code>size</code> and <code>length</code> have the same implementation, depending on the class, they are either independently defined methods, or the former is an alias of the latter. Particularly for <code>Array</code>:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="p">[].</span><span class="nf">method</span><span class="p">(</span><span class="ss">:size</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :length</span>
<span class="p">[].</span><span class="nf">method</span><span class="p">(</span><span class="ss">:length</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :length</span>
</code></pre>
<p>but for <code>Hash</code>, <code>String</code>, and <code>Symbol</code>:</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="p">{}.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:size</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :size</span>
<span class="p">{}.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:length</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :length</span>
<span class="s2">""</span><span class="p">.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:size</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :size</span>
<span class="s2">""</span><span class="p">.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:length</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :length</span>
<span class="ss">:""</span><span class="p">.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:size</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :size</span>
<span class="ss">:""</span><span class="p">.</span><span class="nf">method</span><span class="p">(</span><span class="ss">:length</span><span class="p">).</span><span class="nf">original_name</span> <span class="c1"># => :length</span>
</code></pre>
<p>This might be a big issue, but since there is a standard Ruby method <code>original_name</code>, which returns different results, whether this being one way or another should matter. And I see no reason why they should behave differently. They should be unified in one way. Perhaps <code>Hash</code>, <code>String</code>, and <code>Symbol</code> should be made in the same way as with <code>Array</code>.</p> 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 - 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 - Feature #12084 (Closed): `Class#instance`https://bugs.ruby-lang.org/issues/120842016-02-18T09:31:05Zsawa (Tsuyoshi Sawada)
<p>For meta-programming/debugging purposes, I would like to request the inverse of <code>Object#singleton_class</code>. Namely, a method that is called on a class that is a singleton class, and returns the object it is a singleton of. Since the <code>Singleton</code> module in the standard library <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> assigns the method name <code>instance</code> to such classes, I think <code>Class#instance</code> should be the name for such feature.</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="no">Array</span><span class="p">.</span><span class="nf">singleton_class</span><span class="p">.</span><span class="nf">instance</span> <span class="c1"># => Array</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">singleton_class</span><span class="p">.</span><span class="nf">instance</span> <span class="c1"># => "foo"</span>
</code></pre>
<p>When the receiver is a class but is not a singleton class, then it should raise an error.</p>
<pre><code class="RUBY syntaxhl" data-language="RUBY"><span class="no">Array</span><span class="p">.</span><span class="nf">instance</span> <span class="c1"># => error</span>
</code></pre> 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 #11878 (Rejected): Comparison of prepended moduleshttps://bugs.ruby-lang.org/issues/118782015-12-26T14:44:08Zsawa (Tsuyoshi Sawada)
<p>Including module <code>B</code> to class/module <code>A</code> gives the following results (as expected):</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">A</span><span class="p">;</span> <span class="k">end</span>
<span class="k">module</span> <span class="nn">B</span><span class="p">;</span> <span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">include</span> <span class="no">B</span>
<span class="no">A</span> <span class="o"><</span> <span class="no">B</span> <span class="c1"># => true</span>
<span class="no">B</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => false</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">B</span> <span class="c1"># => -1</span>
</code></pre>
<p>And prepending module <code>C</code> to <code>A</code> gives the following results:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">module</span> <span class="nn">C</span><span class="p">;</span> <span class="k">end</span>
<span class="no">A</span><span class="p">.</span><span class="nf">prepend</span> <span class="no">C</span>
<span class="no">A</span> <span class="o"><</span> <span class="no">C</span> <span class="c1"># => true</span>
<span class="no">C</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => nil</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">C</span> <span class="c1"># => -1</span>
</code></pre>
<p>It looks like including and prepending almost do not make difference with respect to module comparison, i.e., <code>A < B</code> and <code>A < C</code> are the same, and <code>A <=> B</code> and <code>A <=> C</code> are the same. However, then, the difference between <code>B < A</code> and <code>C < A</code> stands out unexplained. I suppose this is a bug. If <code>C < A</code> were to return <code>false</code>, then it would be at least consistent.</p>
<p>However, if that was what was intended, then at least to me, it is strange. In that case, I would like to make this a feature request. I would rather expect:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">A</span> <span class="o"><</span> <span class="no">C</span> <span class="c1"># => false</span>
<span class="no">C</span> <span class="o"><</span> <span class="no">A</span> <span class="c1"># => true</span>
<span class="no">A</span> <span class="o"><=></span> <span class="no">C</span> <span class="c1"># => 1</span>
</code></pre> Ruby master - Bug #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 #11860 (Closed): Double splat does not work on empty hash assigned via variablehttps://bugs.ruby-lang.org/issues/118602015-12-22T08:33:54Zsawa (Tsuyoshi Sawada)
<p>When an empty hash is given as a literal, the double splat operates on it, and leaves nothing, which is expected.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">String</span>
<span class="k">def</span> <span class="nf">foo</span><span class="p">;</span> <span class="k">end</span>
<span class="k">end</span>
<span class="p">[</span><span class="o">**</span><span class="p">{}]</span> <span class="c1"># => []</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">foo</span><span class="p">(</span><span class="o">**</span><span class="p">{})</span> <span class="c1"># => nil</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="o">**</span><span class="p">{})</span> <span class="c1"># => nil</span>
</code></pre>
<p>However, when an empty hash is given via variable, the double splat retains an empty hash in place.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">h</span> <span class="o">=</span> <span class="p">{}</span>
<span class="p">[</span><span class="o">**</span><span class="n">h</span><span class="p">]</span> <span class="c1"># => [{}]</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">foo</span><span class="p">(</span><span class="o">**</span><span class="n">h</span><span class="p">)</span> <span class="c1"># => wrong number of arguments (given 1, expected 0)</span>
<span class="s2">"foo"</span><span class="p">.</span><span class="nf">send</span><span class="p">(</span><span class="s2">"foo"</span><span class="p">,</span> <span class="o">**</span><span class="n">h</span><span class="p">)</span> <span class="c1"># => wrong number of arguments (given 1, expected 0)</span>
</code></pre> Ruby master - Feature #11818 (Closed): `Hash#compact`https://bugs.ruby-lang.org/issues/118182015-12-15T05:09:54Zsawa (Tsuyoshi Sawada)
<p>I request <code>Hash#compact</code> and <code>Hash#compact!</code> that remove the key-value pairs whose value is <code>nil</code>, as follows:</p>
<pre><code>h1 = {a:, 1, b: nil, c: 2}
h1.compact # => {a: 1, c: 2}
h1 # => {a: 1, b: nil, c: 2}
h2 = {a:, 1, b: nil, c: 2}
h2.compact! # => {a: 1, c: 2}
h2 # => {a: 1, c: 2}
h3 = {a:, 1, c: 2}
h3.compact! # => nil
h3 # => {a: 1, c: 2}
</code></pre>
<p>I believe people have frequent need to do this.</p> Ruby master - Feature #11813 (Rejected): Extend safe navigation operator for [] and []= with synt...https://bugs.ruby-lang.org/issues/118132015-12-13T08:09:31Zsawa (Tsuyoshi Sawada)
<p>Now we have the safe navigation operator <code>&.</code>. But this cannot be used with syntax sugar form of the methods <code>[]</code> and <code>[]=</code>, which are more frequent than their ordinary forms of method call. For example, when <code>a</code> can be either an array or <code>nil</code>, we can do:</p>
<pre><code>a &.[](3)
a &.[]= 2, :foo
</code></pre>
<p>but we cannot do:</p>
<pre><code>a &.[3]
a &.[2] = :foo
</code></pre>
<p>It would be nice if we can extend the use of <code>&.</code> to cover syntactic sugar as above.</p> Ruby master - Bug #11805 (Closed): Cannot run Rubyhttps://bugs.ruby-lang.org/issues/118052015-12-11T10:24:09Zsawa (Tsuyoshi Sawada)
<p>I installed the new Ruby, and tried to run Ruby, but it raises the following error.</p>
<p>(Since I installed Ruby 2.3.0preview1, it had a bug, and I had not been able to run Ruby, and now, after having waited for a month, I installed the new one, and there still is a bug. Disappointed a little bit.)</p>
<pre><code>/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `initialize': Permission denied @ rb_sysopen - /usr/local/lib/ruby/gems/2.3.0/specifications/did_you_mean-1.0.0.beta3.gemspec (Errno::EACCES)
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `open'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:112:in `data'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/stub_specification.rb:203:in `valid?'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:748:in `select'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:748:in `gemspec_stubs_in'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:773:in `block in map_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `each'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `flat_map'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:770:in `map_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:762:in `installed_stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:830:in `stubs'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/specification.rb:1035:in `find_by_path'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems.rb:188:in `try_activate'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:124:in `rescue in require'
from /usr/local/lib/ruby/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:39:in `require'
from <internal:gem_prelude>:6:in `<internal:gem_prelude>'
</code></pre> Ruby master - Feature #11797 (Closed): `Enumerator#with_object` with multiple objectshttps://bugs.ruby-lang.org/issues/117972015-12-09T16:15:51Zsawa (Tsuyoshi Sawada)
<p>Sometimes, when working with <code>Enumerator#with_object</code>, I want to keep some additional temporary objects besides the one to return. A use case is as follows (I got this from this StackOverflow question: <a href="http://stackoverflow.com/questions/3418123" class="external">http://stackoverflow.com/questions/3418123</a>). Suppose I have an enumerator created from an array:</p>
<pre><code>e = ["a", "b", "c", "c", "a", "c"].to_enum
</code></pre>
<p>and want to get an array of its repeated elements in the order they are repeated (i.e., appears for the second time):</p>
<pre><code># => ["c", "a"]
</code></pre>
<p>I can do it using <code>Enumerator#with_object</code> like this:</p>
<pre><code>e.to_enum.with_object([{}, []]){|c, (h, a)| h[c] ? a.push(c) : h.store(c, true)}.last.uniq
</code></pre>
<p>Here, I am getting the array referred to as <code>a</code> in the block, but besides that, I am using a temporal hash <code>h</code>. I thought it would be nice if <code>Enumerator#with_object</code> accepts one or more objects, pass them individually as block arguments, and returns only the last argument so that I can do this:</p>
<pre><code>e.to_enum.with_object({}, []){|c, h, a| h[c] ? a.push(c) : h.store(c, true)}.uniq
</code></pre> Ruby master - Feature #11761 (Rejected): `Hash#default_set` and `Hash#default_proc_set`https://bugs.ruby-lang.org/issues/117612015-12-02T06:34:55Zsawa (Tsuyoshi Sawada)
<p>I often want to assign a default value or proc to an existing hash <code>h</code> and chain method after it. The way I do it is:</p>
<pre><code>Hash.new(default_value).merge(h)...
Hash.new{default_proc_value}.merge(h)...
</code></pre>
<p>It would be convenient if I can assign them to a hash directly, and <code>Hash#default=</code> and <code>Hash#default_proc=</code> are close to doing that, but they are actually not useful because the return value is not the receiver hash:</p>
<pre><code>h.default = default_value; h...
h.default_proc = proc {default_proc_value}; h...
</code></pre>
<p>I wish there were methods <code>Hash#default_set</code> and <code>Hash#default_proc_set</code> that return the receiver hash:</p>
<pre><code>h.default_set(default_value)...
h.default_proc_set{default_proc_value}...
</code></pre> Ruby master - 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 #10797 (Closed): `inherit` parameter for `..._defined?` methods in Modulehttps://bugs.ruby-lang.org/issues/107972015-01-28T22:27:49Zsawa (Tsuyoshi Sawada)
<p><code>Module#const_defined?</code> takes an optional argument <code>inherit</code> with default value <code>true</code> to control whether to consider inheritance. But the following methods do not:</p>
<ul>
<li><code>Module#method_defined?</code></li>
<li><code>Module#private_method_defined?</code></li>
<li><code>Module#protected_method_defined?</code></li>
<li><code>Module#public_method_defined?</code></li>
</ul>
<p>They only allow what would correspond to <code>inherit = true</code>. I request an optional argument <code>inherit</code> with default value <code>true</code> to be added to these methods to control whether inheritance should be considered.</p> Ruby master - Feature #10771 (Closed): An easy way to get the source location of a constanthttps://bugs.ruby-lang.org/issues/107712015-01-22T05:50:18Zsawa (Tsuyoshi Sawada)
<p>For constants, it is difficult to get the source location where it was (last) defined. I request either of the following to be implemented:</p>
<ul>
<li>
<p>Tracepoint emits a signal when a constant is defined.</p>
</li>
<li>
<p>Implement a <code>Constant</code> class (similar to <code>Method</code> class) and a <code>constant</code> method (similar to <code>method</code> method) that behave as follows:</p>
<pre><code> foo1.rb
1| module Foo
2| Bar = :bar
3| end
4| Foo.constant(:Bar) #=> #<Constant: Foo#Bar>
5| Foo.constant(:Bar).source_location #=> ["foo1.rb", 2]
</code></pre>
</li>
<li>
<p>Implement <code>Module#constant_source_location</code></p>
<pre><code> foo2.rb
1| module Foo
2| Bar = :bar
3| end
4| Foo.constant_source_location(:Bar) #=> ["foo2.rb", 2]
</code></pre>
</li>
</ul> Ruby master - Feature #10729 (Open): Array method to subtract in placehttps://bugs.ruby-lang.org/issues/107292015-01-10T22:36:02Zsawa (Tsuyoshi Sawada)
<p>I request a method on array that takes another array, subtract that from self in place (= destructively), and return the subtracted elements:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method([2, 4, 6]) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>Ideally, it should also allow a block:</p>
<pre><code>a = [1, 2, 3, 4, 5]
a.some_method(&:even?) #=> [2, 4]
a #=> [1, 3, 5]
</code></pre>
<p>This operation is quite frequent, but at the moment, it requires several steps to do.</p> Ruby master - Feature #10482 (Feedback): Allow ignored items to vary in `Enumerable#chunk`.https://bugs.ruby-lang.org/issues/104822014-11-06T07:56:37Zsawa (Tsuyoshi Sawada)
<p>In <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Combined map/select method (Closed)" href="https://bugs.ruby-lang.org/issues/5663">#5663</a>, regarding a method proposed, Yehuda Katz's writes:</p>
<pre><code>The only caveat is that it would be impossible to intentionally return nil here; suggestions welcome.
</code></pre>
<p>I would like to note here that the same problem exists with <code>Enumerable#chunk</code>. Currently, when the key value is <code>nil</code>, the corresponding items are thrown out. That may be useful sometimes, but sometimes, silently doing so causes a hard-to-detect bug. At least, there should be a way to change what is ignored (which would not break existing code using it), and ideally, nothing should be thrown out unless explicitly specified (which would break existing code).</p>
<p>I propose <code>Enumerable#chunk</code> to take an optional named parameter <code>ignore</code>, which switches what is ignored. When something other than <code>nil</code> is specified, then <code>nil</code> should not be ignored:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="ss">:foo1</span><span class="p">,</span> <span class="ss">:foo2</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">chunk</span><span class="p">(</span><span class="ss">ignore: </span><span class="no">String</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="p">.</span><span class="nf">class</span><span class="p">}</span>
<span class="c1"># => [[Symbol, [:foo1, :foo2]], [NilClass, [nil, nil]]]</span>
</code></pre>
<p>When you don't want anything to be ignored, then the parameter should be set to something that does not appear in the receiver:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="ss">:foo1</span><span class="p">,</span> <span class="ss">:foo2</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">,</span> <span class="kp">nil</span><span class="p">,</span> <span class="kp">nil</span><span class="p">].</span><span class="nf">chunk</span><span class="p">(</span><span class="ss">ignore: </span><span class="s2">"nothing to ignore"</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span><span class="p">.</span><span class="nf">class</span><span class="p">}</span>
<span class="c1"># => [[Symbol, [:foo1, :foo2]], [String, ["bar"]], [NilClass, [nil, nil]]]</span>
</code></pre> Ruby master - Feature #10463 (Rejected): :~@ and :!@ are not parsed correctlyhttps://bugs.ruby-lang.org/issues/104632014-11-01T02:13:11Zsawa (Tsuyoshi Sawada)
<p>The at mark in literal symbols <code>:~@</code> and <code>:!@</code> are ignored.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="ss">:~</span><span class="err">@</span> <span class="c1"># => :~</span>
<span class="p">:</span><span class="o">!</span><span class="err">@</span> <span class="c1"># => :!</span>
</code></pre> Ruby master - Feature #10426 (Open): A predicate to express congruencehttps://bugs.ruby-lang.org/issues/104262014-10-26T00:55:20Zsawa (Tsuyoshi Sawada)
<p>I occasionally felt the necessity of a predicate that checks congruence with respect to some operations:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Object</span>
<span class="k">def</span> <span class="nf">equal_by?</span> <span class="n">other</span><span class="p">,</span> <span class="o">&</span><span class="n">pr</span>
<span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="nb">self</span><span class="p">)</span> <span class="o">==</span> <span class="n">pr</span><span class="p">.</span><span class="nf">call</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">alias</span> <span class="n">congruent?</span> <span class="n">equal_by?</span>
<span class="k">end</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">congruent?</span><span class="p">(</span><span class="mi">2</span><span class="p">){</span><span class="o">|</span><span class="n">e</span><span class="o">|</span> <span class="n">e</span> <span class="o">%</span> <span class="mi">3</span><span class="p">}</span> <span class="c1">#=> true</span>
<span class="s2">"HELLO"</span><span class="p">.</span><span class="nf">equal_by?</span><span class="p">(</span><span class="s2">"Hello"</span><span class="p">,</span> <span class="o">&</span><span class="ss">:downcase</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre> Ruby master - Feature #10425 (Open): A predicate method to tell if a number is near anotherhttps://bugs.ruby-lang.org/issues/104252014-10-26T00:38:58Zsawa (Tsuyoshi Sawada)
<p>A method <code>near?</code> like the following would be useful.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Numeric</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span> <span class="o">-</span> <span class="n">to_f</span><span class="p">).</span><span class="nf">abs</span> <span class="o"><=</span> <span class="n">delta</span><span class="p">.</span><span class="nf">to_f</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="k">class</span> <span class="nc">Time</span>
<span class="k">def</span> <span class="nf">near?</span> <span class="n">other</span><span class="p">,</span> <span class="ss">delta: </span><span class="no">Float</span><span class="o">::</span><span class="no">EPSILON</span>
<span class="n">to_f</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">other</span><span class="p">.</span><span class="nf">to_f</span><span class="p">,</span> <span class="ss">delta: </span><span class="n">delta</span><span class="p">)</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p>It can be used to check errors, or whether something is around something.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">23.24324</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mf">23.23</span><span class="p">,</span> <span class="ss">delta: </span><span class="mf">0.5</span><span class="p">)</span> <span class="c1"># => true</span>
<span class="n">t1</span> <span class="o">=</span> <span class="n">t2</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t3</span> <span class="o">=</span> <span class="no">Time</span><span class="p">.</span><span class="nf">now</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t2</span><span class="p">)</span> <span class="c1">#=> true</span>
<span class="n">t1</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="n">t3</span><span class="p">)</span> <span class="c1">#=> false</span>
<span class="mi">5</span><span class="p">.</span><span class="nf">near?</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="ss">delta: </span><span class="mi">1</span><span class="p">)</span> <span class="c1">#=> true</span>
</code></pre>
<p>Some testing frameworks have something similar to this, but I think this is an elementary concept that Ruby should support at it core.</p> Ruby master - Feature #10394 (Feedback): An instance method on Enumerator that evaluates the bloc...https://bugs.ruby-lang.org/issues/103942014-10-16T14:23:15Zsawa (Tsuyoshi Sawada)
<p><strong>Background</strong></p>
<p>There has been desire to omit the <code>| |</code> and the explicit receiver in a block used with an enumerator or an enumerable. Currently, when the content of the block is a single method that takes no argument, symbol-to-proc is used with the <code>&</code> syntax so that:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>can be written as:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">(</span><span class="o">&</span><span class="ss">:upcase</span><span class="p">)</span>
</code></pre>
<p>There has repeated been proposals (<a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: map/collect extension which handles arguments (Open)" href="https://bugs.ruby-lang.org/issues/8987">#8987</a>, <a class="issue tracker-2 status-7 priority-4 priority-default closed" title="Feature: New one-argument block syntax: &. (Feedback)" href="https://bugs.ruby-lang.org/issues/9076">#9076</a>, <a class="issue tracker-2 status-1 priority-4 priority-default" title="Feature: [PATCH 0/n] Let underscore be positionally matched arg to omit binding obvious variable. (Open)" href="https://bugs.ruby-lang.org/issues/10318">#10318</a>) that express this desire to do this even when the block involves a method chain or a method with arguments like the following:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">concat</span><span class="p">(</span><span class="s2">"ber"</span><span class="p">)}</span>
<span class="p">[</span><span class="s2">" foo "</span><span class="p">,</span> <span class="s2">"</span><span class="se">\t</span><span class="s2">bar</span><span class="se">\n</span><span class="s2">"</span><span class="p">].</span><span class="nf">map</span><span class="p">{</span><span class="o">|</span><span class="n">s</span><span class="o">|</span> <span class="n">s</span><span class="p">.</span><span class="nf">strip</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>Focus has been on modifying how a block is passed to the enumerable/enumerator, and there has not been consensus on how the syntax should be.</p>
<p><strong>Proposal</strong></p>
<p>Unlike the earlier proposals, I suggest that there should be an instance method on <code>Enumerator</code>, let's say <code>Enumerator#as_self</code>, that evaluates the block each time with <code>self</code> being the block variable that would be passed otherwise. With such method, the cases above would be written like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[</span><span class="s2">"foo"</span><span class="p">,</span> <span class="s2">"bar"</span><span class="p">].</span><span class="nf">map</span><span class="p">.</span><span class="nf">as_self</span><span class="p">{</span><span class="n">concat</span><span class="p">(</span><span class="s2">"ber"</span><span class="p">)}</span>
<span class="p">[</span><span class="s2">" foo "</span><span class="p">,</span> <span class="s2">"</span><span class="se">\t</span><span class="s2">bar</span><span class="se">\n</span><span class="s2">"</span><span class="p">].</span><span class="nf">map</span><span class="p">.</span><span class="nf">as_self</span><span class="p">{</span><span class="n">strip</span><span class="p">.</span><span class="nf">upcase</span><span class="p">}</span>
</code></pre>
<p>This adds no modification to the syntax, it just requires a new method <code>Enumerator#as_self</code> to be implemented. I consider this method being along the lines of <code>Enumerator#with_index</code>, <code>Enumerator#with_object</code>; it intervenes between an enumerator (related to a block-taking method) and a block, and let the block-taking method work in a modified way.</p>
<p>It resembles <code>instance_eval</code>, but is different in that it assigns to <code>self</code> what would be a block variable (which changes for each iteration), instead of assigning the receiver.</p> Ruby master - 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 - Bug #10184 (Closed): irb terminates with no method errorhttps://bugs.ruby-lang.org/issues/101842014-08-29T16:33:54Zsawa (Tsuyoshi Sawada)
<p>When I input the following lines in irb, the irb terminates.</p>
<pre><code>$ irb
irb(main):001:0> def !; end
=> :!
irb(main):002:0> !
/usr/local/lib/ruby/2.1.0/irb/input-method.rb:153:in `gets': private method `!' called for false:FalseClass (NoMethodError)
...
$
</code></pre>
<p>A corresponding question is posted on StackOverflow at <a href="http://stackoverflow.com/questions/25572126/irb-terminates-with-no-method-error/25572313#25572313" class="external">http://stackoverflow.com/questions/25572126/irb-terminates-with-no-method-error/25572313#25572313</a>.</p> Ruby master - Feature #10183 (Open): An alternative name for method `class`https://bugs.ruby-lang.org/issues/101832014-08-29T12:52:13Zsawa (Tsuyoshi Sawada)
<p>The method <code>class</code> is special in that it always has to have an explicit receiver in order to avoid crash with the keyword <code>class</code>. But this is very inconvenient. I have seen so many</p>
<pre><code>self.class
</code></pre>
<p>in codes. I propose that there should be an alternative name for this method so that it can be used with an implicit receiver, and the method name <code>class</code> should be gradually depricated.</p>
<p>As for the name, I have no clear idea. I can only think of <code>klass</code>, but perhaps someone else might come up with a better name.</p> Ruby master - Feature #10131 (Feedback): Greatest multiple of r not greater than xhttps://bugs.ruby-lang.org/issues/101312014-08-14T06:32:16Zsawa (Tsuyoshi Sawada)
<p>It happens too often to me that I want to calculate the greatest multiple of numeric r not greater than numeric x. This can be done by a simple operation below:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">x</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="mf">3.7</span><span class="p">,</span> <span class="mf">0.5</span>
<span class="n">x</span><span class="p">.</span><span class="nf">div</span><span class="p">(</span><span class="n">r</span><span class="p">).</span><span class="nf">*</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="c1">#=> 3.5</span>
<span class="n">x</span><span class="p">,</span> <span class="n">r</span> <span class="o">=</span> <span class="mi">41</span><span class="p">,</span> <span class="mi">6</span>
<span class="n">x</span><span class="p">.</span><span class="nf">div</span><span class="p">(</span><span class="n">r</span><span class="p">).</span><span class="nf">*</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="c1">#=> 36</span>
</code></pre>
<p>but I encounter this operation so often that I think it is an essential operation, and should be done as a single operation. Can any of the existing zero-ary operation be extended to take an optional argument to do this calculation? For example</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="mf">3.7</span><span class="p">.</span><span class="nf">floor</span><span class="p">(</span><span class="mf">0.5</span><span class="p">)</span> <span class="c1">#=> 3.5</span>
<span class="mi">41</span><span class="p">.</span><span class="nf">floor</span><span class="p">(</span><span class="mi">6</span><span class="p">)</span> <span class="c1">#=> 36</span>
</code></pre> Ruby master - Feature #10118 (Closed): Double splat for non-symbol keyshttps://bugs.ruby-lang.org/issues/101182014-08-07T23:45:13Zsawa (Tsuyoshi Sawada)
<p>The double splat operator ** only seems to work with hashes whose keys are symbols. It will not work when a key is a string, for example. This is true for both ways; for construction:</p>
<pre><code>def foo **; end
foo(:a => 3) #=> nil
foo("a" => 3) #=> ArgumentError: wrong number of arguments (1 for 0)
</code></pre>
<p>and destruction:</p>
<pre><code>def bar *; end
bar(**{:a => 3}) #=> nil
bar(**{"a" => 3}) #=> TypeError: wrong argument type String (expected Symbol)
</code></pre>
<p>This is confusing. I propose that the double splat syntax should be extended so that it works even when the keys are not symbols.</p> Ruby master - Bug #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 - 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 #9587 (Closed): Integer#times with optional starting valuehttps://bugs.ruby-lang.org/issues/95872014-03-02T19:52:38Zsawa (Tsuyoshi Sawada)
<p>Just like <code>Enumerator#with_index</code> takes an optional argument that specifies the initial value of the index, I would like to request that <code>Integer#times</code> take an optional argument that specifies the initial value. The usefulness of it is similar to that of <code>with_index</code> taking an argument. We sometimes want to repeat tasks a given number of times, and want to use an index not necessarily starting from <code>0</code>.</p>
<pre><code>6.times(1){|i| puts "Chapter #{i}"}
</code></pre>
<p>should give</p>
<pre><code>Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
</code></pre>
<p>with the return value <code>6</code>. We can do it with <code>1.upto(6)</code>, or with <code>#{i + 1}</code> within the block, but giving the initial value to <code>times</code> is much easier.</p> Ruby master - Bug #9581 (Closed): `=~` defined on a subclass of `String` is sometimes ignored, an...https://bugs.ruby-lang.org/issues/95812014-02-28T21:21:32Zsawa (Tsuyoshi Sawada)
<p>As is reported on StackOverflow (<a href="http://stackoverflow.com/questions/22103018" class="external">http://stackoverflow.com/questions/22103018</a>) by Gabriel, overridden <code>=~</code> on a subclass of <code>String</code> is sometimes ignored, and the original <code>String#=~</code> is called. Particularly, when we have:</p>
<pre><code>class MyString < String
def =~ re; :foo end
end
s = MyString.new("abc")
</code></pre>
<p>these give the correct result:</p>
<pre><code>r = /abc/; s =~ r # => :foo
s.send(:=~, r) # => :foo
s.send(:=~, /abc/) # => :foo
</code></pre>
<p>but in this case, <code>MyString#=~</code> is ignored, and <code>String#=~</code> is called instead:</p>
<pre><code>s =~ /abc/ # => 0
</code></pre> Ruby master - Feature #9557 (Open): Enumerator#next and Enumerator#peek with argumenthttps://bugs.ruby-lang.org/issues/95572014-02-23T09:14:18Zsawa (Tsuyoshi Sawada)
<p>It often happens that I want to move the current index of an enumerator by some arbitrary number <code>n</code>. <code>Enumerator#feed</code> takes the element as the argument, but that cannot be used if the enumerator has duplicate elements, or when I do not have information of a particular element to choose but just want to increment the index by some number. <code>Enumerator#next</code>, on the other hand, has a fixed value <code>1</code> to be incremented. It would be convenient if <code>Enumerator#next</code> takes an optional argument that represents the difference of the index to be incremented. The argument can be understood to be defaulted to <code>1</code> when absent.</p>
<p>Also, I often want to look not necessarily the current position, but some position away. It would be good if <code>Enumerator#peek</code> takes an optional argument that represents the positional difference to be peeked. The argument can be understood to be defaulted to <code>0</code> when absent.</p>
<pre><code>enum = [0, 1, 2, 3, 4, 5, 6, 7, 8].to_enum
enum.peek # => 0
enum.peek(0) # => 0
enum.peek(1) # => 1
enum.peek # => 0
enum.next # => 0
enum.next(1) # => 1
enum.next(2) # => 2
enum.peek # => 4
enum.peek(0) # => 4
enum.peek(1) # => 5
enum.peek # => 4
enum.next # => 4
enum.next(1) # => 5
enum.next(2) # => 6
peek # => 8
</code></pre> Ruby master - Feature #9515 (Open): `IO.each` and `CSV.each`https://bugs.ruby-lang.org/issues/95152014-02-12T16:16:41Zsawa (Tsuyoshi Sawada)
<p>In <code>IO</code> class, there are pairs of a class method and an instance method with related function:</p>
<ul>
<li>
<code>IO.read</code> and <code>IO#read</code>
</li>
<li>
<code>IO.write</code> and <code>IO#write</code>
</li>
<li>
<code>IO.foreach</code> and <code>IO#each</code> (or its alias <code>IO#each_line</code>)</li>
</ul>
<p>For consistency, please make <code>IO.each</code> an alias for <code>IO.foreach</code>.</p>
<p>The same thing can be said for <code>CSV.each</code>.</p> Ruby master - 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 - 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 #8970 (Open): Array.zip and Array.producthttps://bugs.ruby-lang.org/issues/89702013-10-01T03:57:09Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
Most of the time when I use <code>Array#zip</code> or <code>Array#product</code>, I feel cumbursome that I have to take out the first array and pass it as a receiver. For example, if I have</p>
<pre><code>a = [[:a, :b, :c], [:d, :e, :f], [:g, :h, :i]]
</code></pre>
<p>I have to do something like this:</p>
<pre><code>a.first.zip(*a.drop(1)){...}
a.first.product(*a.drop(1)){...}
</code></pre>
<p>Sometimes, the receiver (i.e., the first array) has significance, but most other times, that breaks asymmetry, making the code look ugly.</p>
<p>I would be happy if we had <code>Array.zip</code> and <code>Array.product</code> in addition so that we can do it like this:</p>
<pre><code>Array.zip(*a){...}
Array.product(*a){...}
</code></pre>
<p>=end</p> Ruby master - Feature #8948 (Assigned): Frozen regexhttps://bugs.ruby-lang.org/issues/89482013-09-25T04:02:37Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
I see that frozen string was accepted for Ruby 2.1, and frozen array and hash are proposed in <a href="https://bugs.ruby-lang.org/issues/8909" class="external">https://bugs.ruby-lang.org/issues/8909</a>. I feel there is even more use case for a frozen regex, i.e., a regex literal that generates a regex only once. It is frequent to have a regex within a frequently repeated portion of code, and generating the same regex each time is a waste of resource. At the moment, we can have a code like:</p>
<pre><code>class Foo
RE1 = /pattern1/
RE2 = /pattern1/
RE3 = /pattern1/
def classify
case self
when RE1 then 1
when RE2 then 2
when RE3 then 3
else 4
end
end
end
</code></pre>
<p>but suppose we have a frozen <code>Regexp</code> literal <code>//f</code>. Then we can write like:</p>
<pre><code>class Foo
def classify
case self
when /pattern1/f then 1
when /pattern1/f then 2
when /pattern1/f then 3
else 4
end
end
end
</code></pre>
<p>=end</p> Ruby master - Feature #8912 (Feedback): Exception.raisehttps://bugs.ruby-lang.org/issues/89122013-09-15T08:53:17Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
When we have a custom exception class with a custom (({initialize})) method whose arity is not (({1})):</p>
<pre><code>class MyException < StandardError
def initialize x, y
super("Something went wrong with #{x.inspect} because #{y}, blah blah")
end
end
</code></pre>
<p>in order to raise it, we have to create a new instance of it explicitly using (({new})), and embed that under (({Kernel#raise})).</p>
<pre><code>raise(MyException.new(:foo, :bar))
</code></pre>
<p>This is inconvenient, and does not look object oriented. I propose that there should be (({Exception#raise})), which is public, so that we can do:</p>
<pre><code>MyException.raise(:foo, :bar)
</code></pre>
<p>A Ruby implementation may be like this:</p>
<pre><code>class Exception
def self.raise *args; Kernel.send(:raise, *args) end
end
</code></pre>
<p>This will disallow us from calling the private method (({Kernel#raise})) (without an explicit receiver) within the context of an (({Exception})) class unless we use (({send})), but I think such use case is rare, and that should not be a problem.<br>
=end</p> Ruby master - Bug #8885 (Rejected): Incorrect time is created for time including leap secondshttps://bugs.ruby-lang.org/issues/88852013-09-10T16:13:11Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>Time.new</code> creates incorrect time when the time includes a leap second.</p>
<pre><code>Time.new(2012, 6, 30, 23, 59, 60)
# => 2012-07-01 00:00:00 +0900 # Wrong. Should be 2012-06-30 23:59:60 +0900
Time.new(2012, 6, 30, 23, 59, 60) == Time.new(2012, 7, 1, 0, 0, 0)
# => true # Wrong. Should be `false`.
</code></pre>
<p>=end</p> Ruby master - Feature #8834 (Open): Kernel#load_relativehttps://bugs.ruby-lang.org/issues/88342013-08-30T10:32:44Zsawa (Tsuyoshi Sawada)
<p>The intended difference between <code>Kernel#require</code> and <code>Kernel#load</code> is that the former is for external libraries and the latter is for Ruby scripts internal to the project. Considering this fact, <code>load</code> should be more likely than <code>require</code> to be used in a situation where you want to call a file through a relative path. Strangely, there is <code>Kernel#require_relative</code>, but no <code>Kernel#load_relative</code>. I request <code>Kernel#load_relative</code>. It is even more necessary than <code>Kernel#require_relative</code>.</p>
<p>It seems to me that people are using <code>Kernel#require_relative</code> when they want to use a relative path, even in the context where they are supposed to use <code>load</code> because of the lack of <code>Kernel#load_relative</code>. I don't think this is a good practice. Furthermore, in cases where you have a file without a <code>.rb</code> or other extention that you want to call via a relative path, there is no good way to do it.</p> Ruby master - Feature #8827 (Closed): A method that flips the receiver and the first argumenthttps://bugs.ruby-lang.org/issues/88272013-08-28T13:18:27Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
If it often happens that we need to pass to a method an argument that is the result of a long chain of operations:</p>
<pre><code>Hash[some_chain_of_operations_that_ends_up_with_an_array]
File.read(some_chain_of_operations_that_ends_up_with_a_string)
YAML.dump(some_chain_of_operations_that_ends_up_with_an_object)
...
</code></pre>
<p>I believe one basic tenet of Ruby is to encourage method chaining, but that practice is discouraged in the examples above. It would be convenient if there is a method (let us call this <code>Object#flip</code>) that flips the receiver and the first argument and sends the method so that the examples above can be written as follows:</p>
<pre><code>some_chain_of_operations_that_ends_up_with_an_array.flip(Hash, :[])
some_chain_of_operations_that_ends_up_with_a_string.flip(File, :read)
some_chain_of_operations_that_ends_up_with_an_object.flip(YAML, :dump)
...
</code></pre>
<p>The implementation in Ruby may be as follows:</p>
<pre><code>class Object
def flip receiver, method, *rest, &pr
receiver.send(method, self, *rest, &pr)
end
end
</code></pre>
<p>It would be good if we can have that as a built-in Ruby method.</p>
<p>=end</p> Ruby master - Feature #8811 (Feedback): Counterpart to `Hash#key?` for `Array`https://bugs.ruby-lang.org/issues/88112013-08-23T04:04:26Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>Hash</code> hash <code>key?</code> to tell if a key exists without checking the value. It would be convenient if there were a counterpart in <code>Array</code>. Suppose it is called <code>Array#index?</code>. Then it should behave as follows:</p>
<pre><code>[1, 2, 3].index?(2) # => true
[1, 2, 3].index?(3) # => false
[1, 2, 3].index?(-3) # => true
[1, 2, 3].index?(-4) # => false
</code></pre>
<p>This is useful when we want to insert/move/delete elements to/from a certain position of an array. Without checking if a value exists, it can be messed up. Implementing a check is cumbersome now. With the proposed method, it would become easy.</p>
<p>=end</p> Ruby master - Feature #8563 (Rejected): Instance variable argumentshttps://bugs.ruby-lang.org/issues/85632013-06-23T04:30:51Zsawa (Tsuyoshi Sawada)
<p>Often times, people want to assign given arguments to instance variables, especially inside the method <code>initialize</code>:</p>
<pre><code class="Ruby syntaxhl" data-language="Ruby"><span class="k">def</span> <span class="nf">initialize</span> <span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">,</span> <span class="n">buz</span>
<span class="vi">@foo</span><span class="p">,</span> <span class="vi">@bar</span><span class="p">,</span> <span class="vi">@buz</span> <span class="o">=</span> <span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">,</span> <span class="n">buz</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>I propose to let method definition take instance variables as arguments so that:</p>
<pre><code class="Ruby syntaxhl" data-language="Ruby"><span class="k">def</span> <span class="nf">initialize</span> <span class="vi">@foo</span><span class="p">,</span> <span class="vi">@bar</span><span class="p">,</span> <span class="vi">@buz</span>
<span class="o">...</span>
<span class="k">end</span>
</code></pre>
<p>would be equivalent as above.</p> Ruby master - Feature #8350 (Rejected): Extending `%{}` notation in string to accept an arrayhttps://bugs.ruby-lang.org/issues/83502013-05-01T02:09:08Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
<code>String#%</code> accepts a hash as an argument and interpolates the values into <code>%{}</code>-notated portions of the string:</p>
<pre><code>"%{b} said %{a} to %{c}" % {a: "Foo", b: "Bar", c: "Baz"} #=> "Bar said Foo to Baz"
</code></pre>
<p>I would like to have it allow an array as well, where the <code>%{}</code>-notation has indices:</p>
<pre><code>"%{1} said %{0} to %{2}" % ["Foo", "Bar", "Baz"] #=> "Bar said Foo to Baz"
</code></pre>
<p>Of course, since the <code>%{}</code> notation interprets its content as a symbol, the following should also work:</p>
<pre><code>"%{1} said %{0} to %{2}" % {:"0" => "Foo", :"1" => "Bar", :"2" => "Baz"} #=> "Bar said Foo to Baz"
</code></pre>
<p>but that would not cause any conflict.<br>
There is also a different usage of <code>String#%</code> with an array argument like this:</p>
<pre><code>"%-5s: %08x" % [ "ID", self.object_id ] #=> "ID : 200e14d6"
</code></pre>
<p>but that does not allow the array elements to appear in the string in an order different from as is in the array. The proposed feature is distinct from this, and adds flexibility.<br>
=end</p> Backport193 - Backport #8210 (Closed): Multibyte character interfering with end-line character wi...https://bugs.ruby-lang.org/issues/82102013-04-03T15:22:57Zsawa (Tsuyoshi Sawada)
<p>=begin<br>
With this regex:</p>
<pre><code>regex1 = /\z/
</code></pre>
<p>the following strings match as expected:</p>
<pre><code>"hello" =~ regex1 # => 5
"こんにちは" =~ regex1 # => 5
</code></pre>
<p>but with these regexes:</p>
<pre><code>regex2 = /#$/?\z/
regex3 = /\n?\z/
</code></pre>
<p>they show difference:</p>
<pre><code>"hello" =~ regex2 # => 5
"hello" =~ regex3 # => 5
"こんにちは" =~ regex2 # => nil
"こんにちは" =~ regex3 # => nil
</code></pre>
<p>The string encoding is UTF-8, and the OS is Linux (i.e., <code>$/</code> is <code>"\n"</code>). I expect them to behave the same, and believe this is a bug.<br>
=end</p> Ruby master - Feature #8172 (Feedback): IndexError-returning counterparts to destructive Array me...https://bugs.ruby-lang.org/issues/81722013-03-27T10:51:46Zsawa (Tsuyoshi Sawada)
<p>There are a few desctructive <code>Array</code> methods that take an index as an argument and silently insert <code>nil</code> if the index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="p">[];</span> <span class="n">a</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="ss">:foo</span><span class="p">;</span> <span class="n">a</span> <span class="c1"># => [nil, :foo]</span>
<span class="p">[].</span><span class="nf">insert</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="ss">:foo</span><span class="p">)</span> <span class="c1"># => [nil, :foo]</span>
<span class="p">[].</span><span class="nf">fill</span><span class="p">(</span><span class="ss">:foo</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># => [nil, :foo]</span>
</code></pre>
<p>Among them, <code>Array#[]</code> has a counterpart that returns an <code>IndexError</code> when the index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[].</span><span class="nf">fetch</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="c1"># => IndexError</span>
</code></pre>
<p>and this is useful to avoid bugs that would be difficult to find if <code>Array#[]</code> were used. However for <code>Array#insert</code> and <code>Array#fill</code>, there are no such counterparts, and that fact that these methods silently insert <code>nil</code> is often the cause of a bug that is difficult to find.</p>
<p>I suggest there should be some versions of these methods that return <code>IndexError</code> when index is out of range:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="p">[].</span><span class="nf">insert!</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="ss">:foo</span><span class="p">)</span> <span class="c1"># => IndexError</span>
<span class="p">[].</span><span class="nf">fill!</span><span class="p">(</span><span class="ss">:foo</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1"># => IndexError</span>
</code></pre>
<p>I believe this would make debugging easier.</p> Ruby master - Feature #5735 (Closed): Extending the use of splat operator to when it is inside a ...https://bugs.ruby-lang.org/issues/57352011-12-09T13:31:02Zsawa (Tsuyoshi Sawada)
<p>Ruby convention of allowing omittion of the curly brackets for the last argument is convenient:</p>
<p>foo(arg1, arg2, 1 => :a, 2 => :b)</p>
<p>Sometimes, I want to pass a hash with some modifications. For example, suppose <code>h = {3 => :c, 4 => :d, 5 => :e}</code> is a hash already defined and that I want to add some key-value pairs as well as overwrite some values of <code>h</code>, such as {3 => :c, 4 => :f, 5 => :e, 6 => :g}, and pass that. The current convention only allows:</p>
<p>foo(arg1, arg2, h.merge(4 => :f, 6 => :g))</p>
<p>but it would be more convenient if a hash preceded by the splat operator is placed in a hash, it is interpreted as part of the hash, allowing notations like:</p>
<p>foo(arg1, arg2, *h, 4 => :f, 6 => :g)</p>
<p>or, if I want to overwrite the hash <code>{4 => :f, 6 => :g}</code> with <code>h</code>, then:</p>
<p>foo(arg1, arg2, 4 => :f, 6 => :g, *h)</p>
<p>Or besides the argument position, in general, usages like the following:</p>
<p>{3 => :h, *h, 4 => :f, 6 => :g}</p>
<p>This is an analogy from the splat operator used within an array:</p>
<p>[1, 2, *[4, 5, 6], 7, 8]</p> Ruby master - Feature #5554 (Rejected): A method that applies self to a Proc if self is a Symbolhttps://bugs.ruby-lang.org/issues/55542011-11-03T03:51:47Zsawa (Tsuyoshi Sawada)
<p>Often, you want to apply a Proc to self if self is a Symbol, but not do anything if otherwise. In this case, something I call Object#desymbolize may be convenient:</p>
<pre><code>proc = ->sym{
case sym
when :small_icon then "16pt"
when :medium_icon then "32pt"
when :large_icon then "64pt"
end
}
:small_icon.desymbolize(&proc) => "16pt"
"18pt".desymbolize(&proc) => "18pt"
</code></pre>
<p>An implementation may be as follows:</p>
<p>class Object<br>
def desymbolize; self end<br>
end<br>
class Symbol<br>
def desymbolize ≺ pr.call(self) end<br>
end</p> Ruby master - Feature #5553 (Rejected): A method for Hash that works differently depending on whe...https://bugs.ruby-lang.org/issues/55532011-11-03T03:41:07Zsawa (Tsuyoshi Sawada)
<p>A method Hash#if_key(key, [default], &pr) which works like the following will be often used, and is useful.</p>
<pre><code>a = {morning: "おはよう", daytime: "こんにちは", evening: "こんばんは", nothing: nil}
a.if_key(:morning){|str| "#{str}世界!"} #=> "おはよう世界!"
a.if_key(:nothing){|str| "#{str}世界!"} #=> "世界!"
a.if_key(:midnight){|str| "#{str}世界!"} #=> nil
a.if_key(:nothing, "どうも"){|str| "#{str}世界!"} #=> "どうも"
</code></pre>
<p>That is, when <code>key' exists, then the corresponding value will be passed to </code>pr'. Otherwise, the given `default' or the implicit default will be returned.</p> Ruby master - Feature #5552 (Rejected): Array#ljust, Array#rjusthttps://bugs.ruby-lang.org/issues/55522011-11-03T03:27:14Zsawa (Tsuyoshi Sawada)
<p>I propose the following methods Array#ljust(length [, pad]), Array#rjust(length [, pad]), whose behaviour is analogous to String#ljust(length [, padstr]), String#rjust(length [, padstr]).</p>
<pre><code>[:a, :b, :c].ljust(2) #=> [:a, :b, :c]
[:a, :b, :c].ljust(5) #=> [:a, :b, :c, nil, nil]
[:a, :b, :c].ljust(5, :x) #=> [:a, :b, :c, :x, :x]
</code></pre> Ruby master - Feature #5551 (Rejected): Enumerable#rdrophttps://bugs.ruby-lang.org/issues/55512011-11-03T03:18:24Zsawa (Tsuyoshi Sawada)
<p>There is Enumerable#drop(n), which drops elements on the left side, but there is no counterpart for dropping the right side. It will be convenient if there is Enumerable#rdrop(n) that drops the elements on the right side.</p>
<pre><code>[1, 2, 3, 4, 5].rdrop(2) # => [1, 2, 3]
</code></pre> Ruby master - Feature #5550 (Rejected): Hash#depth, Hash#flat_length for recursive hasheshttps://bugs.ruby-lang.org/issues/55502011-11-03T03:10:52Zsawa (Tsuyoshi Sawada)
<p>I often have a hash whose value is recursively a hash, which may look like the following:</p>
<pre><code>{"Japan" =>
{"Hokkaido" => "Sapporo", ...},
{"Honhuu" =>
{"Aomori" => "Hirosaki", ...},
{"Akita" => ...},
...
},
{"Shikoku" => ...},
...
}
</code></pre>
<p>In these cases, it will be convenient if there is a way to know the (maximum) depth of he original hash, and the numbers of all the "terminal nodes". I would like to propose two methods Hash#depth and Hash#flat_length, whose Ruby implementation can be as follows:</p>
<pre><code>class Hash
def depth
1 + (values.map{|v| Hash === v ? v.depth : 1}.max)
end
def flat_length
values.inject(0){|sum, v| sum + (Hash === v ? v.flat_length : 1)}
end
end
</code></pre> Ruby master - Feature #5549 (Rejected): Comparable#min, Comparable#maxhttps://bugs.ruby-lang.org/issues/55492011-11-03T02:45:19Zsawa (Tsuyoshi Sawada)
<p>I request the following methods</p>
<pre><code>Comparable#min(other)
Comparable#max(other)
</code></pre>
<p>which will work the same as</p>
<pre><code>[self, other].min
[self, other].max
</code></pre>
<p>and can be defined as</p>
<pre><code>module Comparable
def min other; [self, other].min end
def max other; [self, other].max end
end
</code></pre>
<p>Example use case: If you need to ensure that some calculated value becomes at least zero within a method chain.</p> Ruby master - Bug #4781 (Closed): Engrish in comment in Regexp#=~https://bugs.ruby-lang.org/issues/47812011-05-26T01:02:09Zsawa (Tsuyoshi Sawada)
<p>I see the following grammatical mistakes in ruby-doc, probably reflecting the comments in the source:</p>
<p>Regexp#=~</p>
<p>wrong: The assignment is not occur if the regexp is not a literal.<br>
correct: The assignment does not occur if the regexp is not a literal.</p>
<p>wrong: The assignment is not occur if the regexp is placed at right hand side.<br>
correct: The assignment does not occur if the regexp is placed at the right hand side.</p>