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.
Incorrect
Section titled “Incorrect”def build(names) result = "" names.each do |name| result += name + ", " end resultendCorrect
Section titled “Correct”def build(names) names.join(", ")endAppend in place with << instead of +=, or build the result once with join.