https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-11-28T16:23:01ZRuby Issue Tracking SystemRuby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=679782017-11-28T16:23:01Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>The issue discussion there is very long.</p>
<p>For the purpose of this thread here, can you list which specific classes<br>
should have ".empty?()"?</p>
<p>String, Array, Hash have this method so far.</p>
<p>I can understand to some extent that <strong>Tempfile</strong> should have this behaviour.</p>
<p>From the thread I see that you also want to include <strong>StringIO</strong> and<br>
<strong>File::Stat</strong>, that is, to have a <strong>.empty?</strong> method.</p>
<p>I do not have any real particular contra opinion; neither do I have a<br>
particular pro opinion. I just would like to have this all in the thread<br>
here, for ease of discussion on MRI. (The rubocop discussion appears to<br>
be more akin to rubocop itself; whereas on MRI one ultimately has to<br>
convince matz and respectively the ruby core team).</p>
<p>Also it may be useful to state why .empty? may be useful for these<br>
classes, for real use cases; I mean, I get the point of avoiding<br>
(.size == 0) checks but I think the MRI team also wants to know<br>
how frequent the usage is.</p>
<p>For String, Array and Hash, that use case is obviously well covered.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=679852017-11-29T00:34:22Zmikegee (Michael Gee)michaelpgee@gmail.com
<ul></ul><p>Sorry, that Rubocop issue does have a bunch of unrelated discussion. I should have summarized the parts I was referring to. Thanks for your feedback.</p>
<p>The discussion began because a user reported Rubocop complaining about this code:</p>
<p><code>File.stat(manifest_file).size == 0</code></p>
<p>Rubocop would prefer that written as <code>File.stat(manifest_file).empty?</code> (Because Rubocop assumes objects with <code>#size</code> and <code>#length</code> also have <code>#empty?</code>, like <code>String</code>, <code>Array</code>, and <code>Hash</code> do.) But, <code>File::Stat</code> does not have an <code>#empty?</code> method, so the suggestion raises <code>NoMethodError</code>.</p>
<p>I agree that the change suggested by Rubocop would improve this code's clarity. The problem is that not all classes with <code>#size</code> and <code>#length</code> also have <code>#empty?</code>.</p>
<p>I claim that adding <code>#empty?</code> to all these classes improves clarity without any significant downside.</p>
<p>I implemented <code>#empty?</code> on the 3 classes mentioned by Rubocop users in that issue.</p>
<p>Thank you for your consideration.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=680062017-11-29T06:52:07Znobu (Nobuyoshi Nakada)nobu@ruby-lang.org
<ul></ul><p>mikegee (Michael Gee) wrote:</p>
<blockquote>
<p>The discussion began because a user reported Rubocop complaining about this code:</p>
<p><code>File.stat(manifest_file).size == 0</code></p>
<p>Rubocop would prefer that written as <code>File.stat(manifest_file).empty?</code> (Because Rubocop assumes objects with <code>#size</code> and <code>#length</code> also have <code>#empty?</code>, like <code>String</code>, <code>Array</code>, and <code>Hash</code> do.) But, <code>File::Stat</code> does not have an <code>#empty?</code> method, so the suggestion raises <code>NoMethodError</code>.</p>
</blockquote>
<p>It's a Rubocop's issue.<br>
I'd suggest <code>File.empty?</code> instead.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=680352017-11-29T09:32:14ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>nobu (Nobuyoshi Nakada) wrote:</p>
<blockquote>
<p>I'd suggest <code>File.empty?</code> instead.</p>
</blockquote>
<p><code>File.empty?</code> might not always work because you might want lstat or other stat objects</p>
<p>but <code>File::Stat#size?</code> might be interesting, it does return nil on empty size</p>
<p><a class="user active user-mention" href="https://bugs.ruby-lang.org/users/8631">@mikegee (Michael Gee)</a> i think you want this: <code>File::Stat#zero?</code></p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=680542017-11-29T14:42:53Zmikegee (Michael Gee)michaelpgee@gmail.com
<ul></ul><p>There seems to be some confusion about what I'm asking for here. I know how to use these classes to make my code work. I'm not asking for help using the existing methods.</p>
<p>I am proposing that all classes that implement <code>#size</code> or <code>#length</code> should also implement <code>#empty?</code> to let developers write clearer code.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=680642017-11-29T21:33:12Zphluid61 (Matthew Kerwin)matthew@kerwin.net.au
<ul></ul><p>mikegee (Michael Gee) wrote:</p>
<blockquote>
<p>I am proposing that all classes that implement <code>#size</code> or <code>#length</code> should also implement <code>#empty?</code> to let developers write clearer code.</p>
</blockquote>
<p>This is one of the Rubocop cops I always disable, because I don't find #empty? conceptually clearer (or necessarily even accurate) unless that's what I wrote in the first place.</p>
<p><code>File.stat</code> is a perfect example: the status object isn't empty. Adding this method would make Ruby code <em>less</em> clear, more idiosyncratic.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=739872018-09-12T05:09:26Zbozhidar (Bozhidar Batsov)bozhidar@batsov.com
<ul></ul><p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>mikegee (Michael Gee) wrote:</p>
<blockquote>
<p>I am proposing that all classes that implement <code>#size</code> or <code>#length</code> should also implement <code>#empty?</code> to let developers write clearer code.</p>
</blockquote>
<p>This is one of the Rubocop cops I always disable, because I don't find #empty? conceptually clearer (or necessarily even accurate) unless that's what I wrote in the first place.</p>
<p><code>File.stat</code> is a perfect example: the status object isn't empty. Adding this method would make Ruby code <em>less</em> clear, more idiosyncratic.</p>
</blockquote>
<p>Yeah, in this case I'd argue that it's better to use some <code>top-level</code> methods of <code>File</code> instead, but in general every object that has the notion of size should also have the option of emptiness. That's common sense and not adhering it to in the default API simply frustrates Ruby developers everywhere.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=739882018-09-12T05:12:08Zbozhidar (Bozhidar Batsov)bozhidar@batsov.com
<ul></ul><p>bozhidar (Bozhidar Batsov) wrote:</p>
<blockquote>
<p>phluid61 (Matthew Kerwin) wrote:</p>
<blockquote>
<p>mikegee (Michael Gee) wrote:</p>
<blockquote>
<p>I am proposing that all classes that implement <code>#size</code> or <code>#length</code> should also implement <code>#empty?</code> to let developers write clearer code.</p>
</blockquote>
<p>This is one of the Rubocop cops I always disable, because I don't find #empty? conceptually clearer (or necessarily even accurate) unless that's what I wrote in the first place.</p>
<p><code>File.stat</code> is a perfect example: the status object isn't empty. Adding this method would make Ruby code <em>less</em> clear, more idiosyncratic.</p>
</blockquote>
<p>Yeah, in this case I'd argue that it's better to use some <code>top-level</code> methods of <code>File</code> instead, but in general every object that has the notion of size should also have the option of emptiness. That's common sense and not adhering it to in the default API simply frustrates Ruby developers everywhere.</p>
</blockquote>
<p>Also I'm curious who'd claim that adding an empty method doesn't make sense for something like <code>Tempfile</code> or <code>StringIO</code>.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=739892018-09-12T05:44:43Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>bozhidar (Bozhidar Batsov) wrote:</p>
<blockquote>
<blockquote>
<p>Yeah, in this case I'd argue that it's better to use some <code>top-level</code> methods of <code>File</code> instead, but in general every object that has the notion of size should also have the option of emptiness. That's common sense and not adhering it to in the default API simply frustrates Ruby developers everywhere.</p>
</blockquote>
</blockquote>
<p>I don't think it's necessarily common sense. It makes sense for collections to have an <code>empty?</code> method. However, not all objects with a <code>size</code> method should necessarily have an implementation of <code>empty?</code>. <code>Pants#size</code> and <code>Horse#size</code> are both methods that could make sense, but <code>Pants#empty?</code> and <code>Horse#empty?</code> may not.</p>
<blockquote>
<p>Also I'm curious who'd claim that adding an empty method doesn't make sense for something like <code>Tempfile</code> or <code>StringIO</code>.</p>
</blockquote>
<p><code>Tempfile#empty?</code> and <code>StringIO#empty?</code> should only be defined if <code>File#empty?</code> is defined, because both <code>Tempfile</code> and <code>StringIO</code> should try to implement the <code>File</code> API to the extent that doing so makes sense. I'm not sure whether <code>File#empty?</code> makes sense. Some people may consider a file of non-zero length with all <code>"\0"</code> or <code>" "</code> bytes to be considered empty. But I guess the same argument could be made that an array of all <code>nil</code> values could be considered empty by some developers.</p>
<p>I will say I haven't been frustrated by the lack of an <code>empty?</code> method on any of the classes being discussed. I'm also of the opinion that adding methods to core/stdlib classes just to appease a static code analyzer is a bad idea. If <code>empty?</code> should be added to any classes, each case should be discussed individually on its own merits, with reasoning given describing why <code>empty?</code> makes semantic sense for the class.</p> Ruby master - Feature #14136: Implement #empty? on more classeshttps://bugs.ruby-lang.org/issues/14136?journal_id=746172018-10-27T07:20:10Zbozhidar (Bozhidar Batsov)bozhidar@batsov.com
<ul></ul><p>jeremyevans0 (Jeremy Evans) wrote:</p>
<blockquote>
<p>bozhidar (Bozhidar Batsov) wrote:</p>
<blockquote>
<blockquote>
<p>Yeah, in this case I'd argue that it's better to use some <code>top-level</code> methods of <code>File</code> instead, but in general every object that has the notion of size should also have the option of emptiness. That's common sense and not adhering it to in the default API simply frustrates Ruby developers everywhere.</p>
</blockquote>
</blockquote>
<p>I don't think it's necessarily common sense. It makes sense for collections to have an <code>empty?</code> method. However, not all objects with a <code>size</code> method should necessarily have an implementation of <code>empty?</code>. <code>Pants#size</code> and <code>Horse#size</code> are both methods that could make sense, but <code>Pants#empty?</code> and <code>Horse#empty?</code> may not.</p>
<blockquote>
<p>Also I'm curious who'd claim that adding an empty method doesn't make sense for something like <code>Tempfile</code> or <code>StringIO</code>.</p>
</blockquote>
<p><code>Tempfile#empty?</code> and <code>StringIO#empty?</code> should only be defined if <code>File#empty?</code> is defined, because both <code>Tempfile</code> and <code>StringIO</code> should try to implement the <code>File</code> API to the extent that doing so makes sense. I'm not sure whether <code>File#empty?</code> makes sense. Some people may consider a file of non-zero length with all <code>"\0"</code> or <code>" "</code> bytes to be considered empty. But I guess the same argument could be made that an array of all <code>nil</code> values could be considered empty by some developers.</p>
<p>I will say I haven't been frustrated by the lack of an <code>empty?</code> method on any of the classes being discussed. I'm also of the opinion that adding methods to core/stdlib classes just to appease a static code analyzer is a bad idea. If <code>empty?</code> should be added to any classes, each case should be discussed individually on its own merits, with reasoning given describing why <code>empty?</code> makes semantic sense for the class.</p>
</blockquote>
<p>Yeah, I completely agree with you. Excellent point about using <code>size</code> in different contexts! Generally I'm not trying to suggest changes for the sake of making RuboCop happy, but for the sake of having more consistent and pleasant APIs.</p>