Rails method to detect an array of empty strings (["," ", ...]) as empty

Is there a rails function to detect ["", "", ...](i.e. an array containing only an empty string or strings) as empty

My requirement:

[""].foo? => true

["", ""].foo? => true

["lorem"].foo? => false

["", "ipsum"].foo? => false

I tried to use array.reject!(&:empty?).blank?. It worked, but it changed my array. I do not want my array to be modified. Please help me find a compact method.

+7
source share
2 answers

There is no 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
+8
source

OP Rails, , Ruby. present? present? blank? Rails ( ActiveSupport, ).

:

[nil, nil].join.empty? # => true
["", nil].join.empty? # => true
["lorem", nil].join.empty? # => false
0

All Articles