- 필수 기능
- 시작하기
- Glossary
- 표준 속성
- Guides
- Agent
- 통합
- 개방형텔레메트리
- 개발자
- Administrator's Guide
- API
- Datadog Mobile App
- CoScreen
- Cloudcraft
- 앱 내
- 서비스 관리
- 인프라스트럭처
- 애플리케이션 성능
- APM
- Continuous Profiler
- 스팬 시각화
- 데이터 스트림 모니터링
- 데이터 작업 모니터링
- 디지털 경험
- 소프트웨어 제공
- 보안
- AI Observability
- 로그 관리
- 관리
ID: ruby-best-practices/no-begin-blocks
Language: Ruby
Severity: Notice
Category: Best Practices
The BEGIN
blocks in Ruby are used to specify pieces of code that should be run before the program is run. These blocks are usually executed before any other code in the current file.
Although BEGIN
blocks can be useful in certain situations, they can also lead to code that is harder to understand and maintain. This is because BEGIN
blocks can create unexpected side effects, especially when they are used in larger codebases where they can be easily overlooked.
To avoid using BEGIN
blocks, you can often move the code to the top of the file, or into a method or function that is explicitly called. This makes the code more explicit, easier to read, and less prone to unexpected side effects. If you need to perform setup steps before running your code, consider using a setup method or function instead.
puts("Running")
BEGIN { puts("Beginning")}
my_str = begin
a = "another "
b = "block"
a + b
end
puts(my_str)
puts("Beginning")
puts("Running")
my_str = begin
a = "another "
b = "block"
a + b
end
puts(my_str)
|
|
For more information, please read the Code Analysis documentation
Identify code vulnerabilities directly in yourVS Code editor
Identify code vulnerabilities directly inJetBrains products