Issue #14136 has been updated by mikegee (Michael Gee).


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.

The discussion began because a user reported Rubocop complaining about this code:

`File.stat(manifest_file).size == 0`

Rubocop would prefer that written as `File.stat(manifest_file).empty?` (Because Rubocop assumes objects with `#size` and `#length` also have `#empty?`, like `String`, `Array`, and `Hash` do.) But, `File::Stat` does not have an `#empty?` method, so the suggestion raises `NoMethodError`.

I agree that the change suggested by Rubocop would improve this code's clarity. The problem is that not all classes with `#size` and `#length` also have `#empty?`.

I claim that adding `#empty?` to all these classes improves clarity without any significant downside.

I implemented `#empty?` on the 3 classes mentioned by Rubocop users in that issue.

Thank you for your consideration.

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

* 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>