From: "knu (Akinori MUSHA)" Date: 2021-09-11T12:32:51+00:00 Subject: [ruby-core:105203] [Ruby master Feature#12075] some container#nonempty? Issue #12075 has been updated by knu (Akinori MUSHA). Here's my opinion about the idea that we should just import ActiveSupport's `present?`. ActiveSupport's `present?` is defined for all kinds of objects, and Rails application programmers are so much used to calling `present?` on any object including what can be evaluated to nil. I think this is probably because `present?` predates the `&.` operator and I guess when it came out it felt handy to be able to test if an object is neither nil nor empty with just one method call. But today, you don't need that because you can just say `array_or_nil&.nonempty?` instead of `array_or_nil.present?`. In other words, we wouldn't need non-container objects, namely nil, to respond to a `present?` method, and this is one reason not to import `present?`. We could just add a new non-emptiness tester method only to container classes. Secondly, `present?` is defined as the opposite of `blank?`, and `string.present?` is not equivalent to `!string.empty?`. So, String would need a different method anyway even if we were to add `present?` only to Array and Hash. I can also point out that `String#blank?` considers a wider set of characters as "blank" than our stock method `String#strip` does, so `String#blank?` as it is now might not fit in the core without reconsideration from the I18n point of view. ---------------------------------------- Feature #12075: some container#nonempty? https://bugs.ruby-lang.org/issues/12075#change-93610 * 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: