https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112016-05-05T11:23:50ZRuby Issue Tracking SystemRuby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584912016-05-05T11:23:50Zka8725 (Andrey Koleshko)ka8725@gmail.com
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/58491/diff?detail_id=41141">diff</a>)</li></ul> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584922016-05-05T11:35:27Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><p>I'm pretty unhappy that community/style guidelines are banning the straightforward and readable solution:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">prices</span><span class="p">.</span><span class="nf">find</span> <span class="p">{</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="nb">p</span><span class="p">.</span><span class="nf">key</span> <span class="o">==</span> <span class="n">size</span> <span class="p">}</span> <span class="ow">or</span> <span class="nb">fail</span> <span class="s2">"Price for </span><span class="si">#{</span><span class="n">size</span><span class="si">}</span><span class="s2"> not found"</span>
</code></pre> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584932016-05-05T13:30:00Zka8725 (Andrey Koleshko)ka8725@gmail.com
<ul></ul><p>Victor Shepelev wrote:</p>
<blockquote>
<p>I'm pretty unhappy that community/style guidelines are banning the straightforward and readable solution:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">prices</span><span class="p">.</span><span class="nf">find</span> <span class="p">{</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="nb">p</span><span class="p">.</span><span class="nf">key</span> <span class="o">==</span> <span class="n">size</span> <span class="p">}</span> <span class="ow">or</span> <span class="nb">fail</span> <span class="s2">"Price for </span><span class="si">#{</span><span class="n">size</span><span class="si">}</span><span class="s2"> not found"</span>
</code></pre>
</blockquote>
<p>I'm not aware about the banning. Could you elaborate what is bad in your example? Basically I don't see anything bad in your code except a little bit verbose like in mine solution.</p> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584942016-05-05T14:05:29Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><blockquote>
<p>Could you elaborate what is bad in your example? Basically I don't see anything bad in your code except a little bit verbose like in mine solution.</p>
</blockquote>
<p>I don't know! The most popular and widely accepted Ruby Style Guide just bans them: <a href="https://github.com/bbatsov/ruby-style-guide#no-and-or-or" class="external">https://github.com/bbatsov/ruby-style-guide#no-and-or-or</a></p>
<p>And when I've tried to rant about how and/or are useful, it all went sideways: <a href="https://www.reddit.com/r/ruby/comments/478a17/rant_on_good_ruby_idiom_andor_operators/" class="external">https://www.reddit.com/r/ruby/comments/478a17/rant_on_good_ruby_idiom_andor_operators/</a></p> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584952016-05-05T14:25:07Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>Victor Shepelev wrote:</p>
<blockquote>
<blockquote>
<p>Could you elaborate what is bad in your example? Basically I don't see anything bad in your code except a little bit verbose like in mine solution.</p>
</blockquote>
<p>I don't know! The most popular and widely accepted Ruby Style Guide just bans them: <a href="https://github.com/bbatsov/ruby-style-guide#no-and-or-or" class="external">https://github.com/bbatsov/ruby-style-guide#no-and-or-or</a></p>
</blockquote>
<p>I don't think it makes a sense, however, you can use <code>||</code> instead if you don't like <code>or</code> of course.</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="n">prices</span><span class="p">.</span><span class="nf">find</span> <span class="p">{</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="nb">p</span><span class="p">.</span><span class="nf">key</span> <span class="o">==</span> <span class="n">size</span> <span class="p">}</span> <span class="o">||</span> <span class="k">raise</span> <span class="s2">"Price for </span><span class="si">#{</span><span class="n">size</span><span class="si">}</span><span class="s2"> not found"</span>
</code></pre>
<p>And it feels better to me because of the error message.</p> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=584962016-05-05T14:46:50Zcremno (cremno phobia)
<ul></ul><p>You can pass a callable object to <code>Enumerable#find</code>:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">def</span> <span class="nf">find_price!</span><span class="p">(</span><span class="n">size</span><span class="p">)</span>
<span class="n">prices</span><span class="p">.</span><span class="nf">find</span><span class="p">(</span><span class="o">-></span><span class="p">{</span> <span class="nb">fail</span> <span class="s2">"Price for </span><span class="si">#{</span><span class="n">size</span><span class="si">}</span><span class="s2"> not found"</span> <span class="p">}){</span> <span class="o">|</span><span class="nb">p</span><span class="o">|</span> <span class="nb">p</span><span class="p">.</span><span class="nf">key</span> <span class="o">==</span> <span class="n">size</span> <span class="p">}</span>
<span class="k">end</span>
</code></pre> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=587792016-05-20T16:43:19Zdsferreira (Daniel Ferreira)
<ul></ul><p>Documentation can be improved to use example of callable object don't you think?<br>
I'm happy to create a PR with it.</p> Ruby master - Feature #12350: Introduce Array#find! that raises an error if element not foundhttps://redmine.ruby-lang.org/issues/12350?journal_id=596532016-07-19T06:20:33Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>I am not against adding a method that raises an exception, but <code>find!</code> is not a proper name for it.<br>
Besides that, we can raise an exception for non-existing values by the current behavior.<br>
So let us start improving documentation. Daniel, could you create an PR?</p>
<p>Matz.</p>