Combine Collection Predicates (ARCH6077)
Ce contenu n’est pas encore disponible dans votre langue.
Key ruby.performance.combine-collection-predicates · Severity info · Category performance
Consecutive select calls or count > 0 for an emptiness check walk the collection multiple times. Fuse the predicates into a single pass.
Incorrect
Section titled “Incorrect”def has_recent_admin?(users) users.select(&:admin?) .select(&:recent?) .count > 0endCorrect
Section titled “Correct”def has_recent_admin?(users) users.any? { |u| u.admin? && u.recent? }endCombine adjacent filters into one predicate and use any? instead of count > 0 for emptiness.