Syntactic sugar to find out if all elements of the true array are returned for a particular method?

In my Ruby program, I have an array of five lines, and I want to check if each of the elements of this array matches a given requirement, for example:

a = ['', '', '', '']
a.inject(:blank?) # Will return true if (and only if) all elements of a are blank

I ask this question because Ruby has a fairly large standard API with a lot of pre-written syntactic sugar that I want to know and do not want to invent.

+3
source share
2 answers

There is a very concise way:

array.all? &:blank?

Learn Enumerableand learn how to use Enumerators , and you will speak the most pleasant Ruby dialect in the blink of an eye.

+15
source

: # to_proc ( , -, ), :

a.inject(&'&& $1.blank?')

a.inject{ |sum,i|
  sum && i.blank?
}
0

All Articles