First Or Default Null (ARCH6078)
Esta página aún no está disponible en tu idioma.
Key ruby.performance.first-or-default-null · Severity warning · Category performance
Taking the first element of a filtered collection with [0] or .first returns nil when the result is empty, and the following access raises NoMethodError. Add an emptiness guard.
Incorrect
Section titled “Incorrect”def first_admin_name(users) users.select(&:admin?).first.nameendCorrect
Section titled “Correct”def first_admin_name(users) admin = users.find(&:admin?) admin&.nameendCheck for nil before using the first element, or use safe navigation (&.).