[ruby-core:104935] [Ruby master Feature#12075] some container#nonempty?
From:
"knu (Akinori MUSHA)" <noreply@...>
Date:
2021-08-17 02:31:31 UTC
List:
ruby-core #104935
Issue #12075 has been updated by knu (Akinori MUSHA).
I tend to like `ary.size > 0` more than `!ary.empty?` because the former literally has a "positive" nuance and therefore it's more readable in many cases. In that sense, the name `nonempty?` does not sound ideal to me.
So, I came up with this. What about `size?`? It's in the existing vocabulary; FileTest and File::Stat has `size?`. It presumably originates from the shell script expression `[ -s file ]` (test(1)) that tests if a file has a size greater than zero. Those methods return nil when the size is zero and return the actual size otherwise, but that's an old convention (cf. `nonempty?`) and we can just add `size?` as a boolean method to Array/Hash/String.
I don't think we should add one to NilClass nor Object (Kernel) in general, but I'm not absolutely sure about what to do with other existing classes with `size`: Integer/MatchData/Range etc. 🤔
----------------------------------------
Feature #12075: some container#nonempty?
https://bugs.ruby-lang.org/issues/12075#change-93300
* Author: naruse (Yui NARUSE)
* Status: Feedback
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
----------------------------------------
I sometimes write following code.
```ruby
ary = some_metho_returns_nil_or_empty_container() # nil or "" or [] or {}
if ary && !ary.empty?
# some code
end
```
But the condition `ary && !ary.empty?` is too long and complex.
Though Ruby 2.3 introduces `&.`, but this can’t be written as `ary&.empty?`.
One idea is add `nonempty?` write as `ary&.nonempty?`.
akr: `nonempty?` is not good name because human is not good at handling
This discussion matches following core classes:
* String
* Array
* Hash
--
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>