- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/concat-strings
Language: Ruby
Severity: Notice
Category: Best Practices
The rule to avoid slow string concatenation in Ruby is essential for writing efficient and fast-performing code. String concatenation using the +=
operator is slower because it creates a new string object every time it’s used. This can lead to performance issues, especially in loops or large programs where numerous string concatenations might be happening.
Instead, the <<
operator, also known as the append operator, should be used for string concatenation in Ruby. The <<
operator modifies the original string, avoiding the creation of multiple unnecessary string objects. This results in faster execution time and lower memory usage, which is especially beneficial in larger applications or systems with limited resources.
Therefore, good coding practice in Ruby suggests using <<
for string concatenation instead of +=
. For instance, output << "<p>#{text}</p>"
is more efficient than output += "<p>#{text}</p>"
. Following this rule will help you write cleaner, faster, and more resource-efficient Ruby code.
output = ''
output += '<h1>Page title</h1>'
output += '<h3>Sub heading</h3>'
texts.each do |text|
output += "<p>#{text}</p>"
end
output = ''
output << '<h1>Page title</h1>'
output << '<h3>Sub heading</h3>'
texts.each do |text|
output << "<p>#{text}</p>"
end
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products