Ir al contenido

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.

def first_admin_name(users)
users.select(&:admin?).first.name
end
def first_admin_name(users)
admin = users.find(&:admin?)
admin&.name
end

Check for nil before using the first element, or use safe navigation (&.).