https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112016-11-22T10:07:30ZRuby Issue Tracking SystemRuby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616112016-11-22T10:07:30Zmudasobwa (Alexei Matyushkin)am@mudasobwa.ru
<ul></ul><p>Stefan Schüßler wrote:</p>
<blockquote>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Integer</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span> <span class="p">{</span> <span class="mi">0</span> <span class="p">}</span> <span class="c1">#=> 0 (same as 'foo'.to_i)</span>
</code></pre>
</blockquote>
<p>To make it possible to fallback to e.g. <code>#to_i</code> it would be great to receive an actual argument as block parameter:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="c1"># ⇓⇓⇓⇓⇓</span>
<span class="no">Integer</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">)</span> <span class="p">{</span> <span class="o">|</span><span class="n">val</span><span class="o">|</span> <span class="n">val</span><span class="p">.</span><span class="nf">to_i</span> <span class="p">}</span>
</code></pre> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616122016-11-22T10:08:55Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/12732">Feature #12732</a>: An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception</i> added</li></ul> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616152016-11-22T10:17:04Zsos4nt (Stefan Schüßler)mail@stefanschuessler.de
<ul></ul><p>Alexei Matyushkin wrote:</p>
<blockquote>
<p>To make it possible to fallback to e.g. <code>#to_i</code> it would be great to receive an actual argument as block parameter:</p>
</blockquote>
<p>Absolutely, I've mentioned that in the last sentence.</p> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616172016-11-22T11:31:34Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>+1 I like this idea. It must also be backwards compatible.</p> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616352016-11-23T03:17:28Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Only "invalid value for Integer()" from a string?<br>
<code>Integer</code> can raise exceptions other than <code>ArgumentError</code> against a string, for example.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="no">Integer</span><span class="p">(</span><span class="kp">nil</span><span class="p">)</span> <span class="c1">#=> can't convert nil into Integer (TypeError)</span>
<span class="no">Integer</span><span class="p">(</span><span class="no">Object</span><span class="p">.</span><span class="nf">new</span><span class="p">)</span> <span class="c1">#=> can't convert Object into Integer (TypeError)</span>
<span class="no">Integer</span><span class="p">(</span><span class="no">Object</span><span class="p">.</span><span class="nf">new</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#=> base specified for non string value (ArgumentError)</span>
<span class="no">Integer</span><span class="p">(</span><span class="s2">""</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="c1">#=> invalid radix 1 (ArgumentError)</span>
<span class="no">X</span><span class="o">=</span><span class="no">Struct</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="ss">:to_i</span><span class="p">)</span>
<span class="no">Integer</span><span class="p">(</span><span class="no">X</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="s2">"a"</span><span class="p">))</span> <span class="c1">#=> can't convert #X to Integer (X#to_i gives String) (TypeError)</span>
</code></pre> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=616592016-11-24T14:09:15Zsos4nt (Stefan Schüßler)mail@stefanschuessler.de
<ul></ul><p>Nobuyoshi Nakada wrote:</p>
<blockquote>
<p>Only "invalid value for Integer()" from a string?</p>
</blockquote>
<p>I don't have a strong opinion on this.</p> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=631442017-02-23T08:43:22Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>We looked at this issue at yesterday's developer meeting.</p>
<p>People there argued that explicit keyword argument is much easier to read than passing a block. They say it is not obvious what the block is going to do. So if the OP is OK we would like to to merge this issue with <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception (Closed)" href="https://redmine.ruby-lang.org/issues/12732">#12732</a>. How do you feel, Stefan?</p> Ruby master - Feature #12968: Allow default value via block for Integer(), Float() and Rational()https://redmine.ruby-lang.org/issues/12968?journal_id=655092017-06-28T17:53:08Zsos4nt (Stefan Schüßler)mail@stefanschuessler.de
<ul></ul><p>shyouhei (Shyouhei Urabe) wrote:</p>
<blockquote>
<p>we would like to to merge this issue with <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: An option to pass to `Integer`, `Float`, to return `nil` instead of raise an exception (Closed)" href="https://redmine.ruby-lang.org/issues/12732">#12732</a>. How do you feel, Stefan?</p>
</blockquote>
<p>I'm fine with with that.</p>