Ir al contenido

String Concat In Loop (ARCH6070)

Esta página aún no está disponible en tu idioma.

Key ruby.performance.string-concat-in-loop · Severity warning · Category performance

Accumulating a string with += inside a loop allocates a new string on every iteration, costing O(n^2). Use a mutable buffer or join the parts instead.

def build(names)
result = ""
names.each do |name|
result += name + ", "
end
result
end
def build(names)
names.join(", ")
end

Append in place with << instead of +=, or build the result once with join.