https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112018-09-09T02:42:47ZRuby Issue Tracking SystemRuby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739422018-09-09T02:42:47Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Feedback</i></li></ul><p>No need to change the constructor. Instead of:</p>
<pre><code>Range.new 0, Float::INFINITY, 3
</code></pre>
<p>Use the shorter</p>
<pre><code>(0...) % 3
</code></pre> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739432018-09-09T02:59:15Zv0dro (Sameer Deshmukh)sameer.deshmukh93@gmail.com
<ul></ul><p>Advantages of changing the constructor:</p>
<ul>
<li>Makes it easy to read test code for someone new to Ruby.</li>
<li>Consistency in specifying step count in constructor (simple and straightforward way) and using<br>
the shorter syntax (idiomatic Ruby way).</li>
</ul>
<p>Disadvantages:</p>
<ul>
<li>Few extra lines of code to change the constructor.</li>
</ul>
<p>I think the advantages outweigh the disadvantages.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739442018-09-09T03:12:27Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>v0dro (Sameer Deshmukh) wrote:</p>
<blockquote>
<p>And I want the values <code>1</code>, <code>4</code>, <code>7</code>, <code>10</code> and <code>12</code>, I can simply specify a Range like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">r</span> <span class="o">=</span> <span class="no">Range</span><span class="p">.</span><span class="nf">new</span> <span class="mi">0</span><span class="p">,</span> <span class="no">Float</span><span class="o">::</span><span class="no">INFINITY</span><span class="p">,</span> <span class="mi">3</span> <span class="c1"># start, stop (upto the end), step</span>
<span class="n">a</span><span class="p">[</span><span class="n">r</span><span class="p">]</span>
<span class="c1"># => NArray([1, 4, 7, 10, 12])</span>
</code></pre>
</blockquote>
<p>Wouldn't the result be <code>[1, 4, 7, 10, 13]</code>?</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739482018-09-09T03:36:36Zv0dro (Sameer Deshmukh)sameer.deshmukh93@gmail.com
<ul></ul><blockquote>
<p>Wouldn't the result be [1, 4, 7, 10, 13]?</p>
</blockquote>
<p>Ah yes. My bad. Editing the description. Thank you.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739492018-09-09T03:37:28Zv0dro (Sameer Deshmukh)sameer.deshmukh93@gmail.com
<ul></ul><p>v0dro (Sameer Deshmukh) wrote:</p>
<blockquote>
<p>I would like to propose making changes to the Range constructor so that a user can specify<br>
a <code>step</code> count along with <code>start</code> and <code>stop</code>. Since Ruby 2.6 will introduce a <code>step</code> property<br>
in Ranges anyway I think this will be a useful addition.</p>
<p>Here's my reasons for the changes:</p>
<p>When creating software libraries for numerical computing, it is common to query the data<br>
container for values in a particular range at some given steps. For example, say I have the<br>
following NArray object:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">a</span> <span class="o">=</span> <span class="no">NArray</span><span class="p">.</span><span class="nf">new</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="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">,</span><span class="mi">7</span><span class="p">,</span><span class="mi">8</span><span class="p">,</span><span class="mi">9</span><span class="p">,</span><span class="mi">10</span><span class="p">,</span><span class="mi">11</span><span class="p">,</span><span class="mi">12</span><span class="p">])</span>
</code></pre>
<p>And I want the values <code>1</code>, <code>4</code>, <code>7</code>, <code>10</code> and <code>12</code>, I can simply specify a Range like this:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">r</span> <span class="o">=</span> <span class="no">Range</span><span class="p">.</span><span class="nf">new</span> <span class="mi">0</span><span class="p">,</span> <span class="no">Float</span><span class="o">::</span><span class="no">INFINITY</span><span class="p">,</span> <span class="mi">3</span> <span class="c1"># start, stop (upto the end), step</span>
<span class="n">a</span><span class="p">[</span><span class="n">r</span><span class="p">]</span>
<span class="c1"># => NArray([1, 4, 7, 10, 12])</span>
</code></pre>
<p>This can possibly also be extended to <code>Array#[]</code> so that users can get ranges of values at<br>
steps without much worry.</p>
</blockquote>
<p>The array will return <code>[1,4,7,10]</code>. Sorry for previous mistake.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739542018-09-09T09:19:08Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><blockquote>
<p>Wouldn't the result be [1, 4, 7, 10, 13]?</p>
</blockquote>
<p>Off-by-one is ... common. :)</p>
<p>A bit more on topic, Float::INFINITY is quite long. Could<br>
we not use :infinity to refer to it in some methods or<br>
something shorter? I think ruby users should not need to<br>
have to know the leading "namespace" (Float) in order to<br>
refer to a concept of infinity in ruby.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739582018-09-09T13:57:41Zmarcandre (Marc-Andre Lafortune)marcandre-ruby-core@marc-andre.ca
<ul></ul><p>v0dro (Sameer Deshmukh) wrote:</p>
<blockquote>
<p>Advantages of changing the constructor:</p>
<ul>
<li>Makes it easy to read test code for someone new to Ruby.</li>
</ul>
</blockquote>
<p>Very debatable.</p>
<blockquote>
<ul>
<li>Consistency in specifying step count in constructor (simple and straightforward way) and using<br>
the shorter syntax (idiomatic Ruby way).</li>
</ul>
</blockquote>
<p>It's actually not consistent, since the result is a <code>Enumerator::ArithmeticSequence</code>, not a <code>Range</code>.</p>
<blockquote>
<p>Disadvantages:</p>
<ul>
<li>Few extra lines of code to change the constructor.</li>
</ul>
</blockquote>
<p>I realize you wrote "introduces a step property in Ranges anyway I think this will be a useful addition", but this is not true. <code>Range</code> will not have a <code>step</code> property. It's simply that the <code>step</code> method will now return a different class of object that is more useful.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739762018-09-11T01:01:26Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/13904">Feature #13904</a>: getter for original information of Enumerator</i> added</li></ul> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739782018-09-11T01:03:23Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>We've already have Emuerator::ArithmeticSequence in trunk.<br>
Please use it.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739792018-09-11T01:16:10Zv0dro (Sameer Deshmukh)sameer.deshmukh93@gmail.com
<ul></ul><p>OK I'm convinced this is probably a bad idea since <code>step</code> returns an <code>ArithmeticSequence</code><br>
and is not a property of <code>Range</code>.</p>
<p>Closing the issue.</p> Ruby master - Feature #15092: Provide step count in Range constructorhttps://redmine.ruby-lang.org/issues/15092?journal_id=739802018-09-11T01:18:10Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Status</strong> changed from <i>Feedback</i> to <i>Rejected</i></li></ul>