Rails method to detect array of empty strings (["", "",...]) as empty
There isn't a single method, but you can use .all?
.
["", nil].all?(&:blank?) # => true
["ipsum", ""].all?(&:blank?) # => false
Or you can get the opposite result with .any?
.
["", nil].any?(&:present?) # => false
["lorem", ""].any?(&:present?) # => true
OP was asking for a solution for Rails but I came here while looking for a generic Ruby solutions. Since present?
and blank?
are both Rails extensions the above solution did not work for me (unless I pull in ActiveSupport which I didn't want).
May I instead offer a simpler solution:
[nil, nil].join.empty? # => true
["", nil].join.empty? # => true
["lorem", nil].join.empty? # => false