Issue #14136 has been updated by jeremyevans0 (Jeremy Evans).


bozhidar (Bozhidar Batsov) wrote:
> > Yeah, in this case I'd argue that it's better to use some `top-level` methods of `File` 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.

I don't think it's necessarily common sense.  It makes sense for collections to have an `empty?` method.  However, not all objects with a `size` method should necessarily have an implementation of `empty?`.  `Pants#size` and `Horse#size` are both methods that could make sense, but `Pants#empty?` and `Horse#empty?` may not.

> Also I'm curious who'd claim that adding an empty method doesn't make sense for something like `Tempfile` or `StringIO`.

`Tempfile#empty?` and `StringIO#empty?` should only be defined if `File#empty?` is defined, because both `Tempfile` and `StringIO` should try to implement the `File` API to the extent that doing so makes sense. I'm not sure whether `File#empty?` makes sense.  Some people may consider a file of non-zero length with all `"\0"` or `" "` bytes to be considered empty. But I guess the same argument could be made that an array of all `nil` values could be considered empty by some developers.

I will say I haven't been frustrated by the lack of an `empty?` 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 `empty?` should be added to any classes, each case should be discussed individually on its own merits, with reasoning given describing why `empty?` makes semantic sense for the class.

----------------------------------------
Feature #14136: Implement #empty? on more classes
https://bugs.ruby-lang.org/issues/14136#change-73989

* Author: mikegee (Michael Gee)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Hi Ruby Friends!

Rubocop prefers `#empty?` over `length == 0` and `size == 0`, which is great for `String`, `Array`, `Hash`, etc. It would be nice if more classes implemented `#empty?` for consistency.

See related discussion at https://github.com/bbatsov/rubocop/issues/2841.

I started this work at https://github.com/ruby/ruby/pull/1759

Thanks!



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>