このページは日本語には対応しておりません。随時翻訳に取り組んでいます。
翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください

Metadata

ID: ruby-best-practices/no-begin-blocks

Language: Ruby

Severity: Notice

Category: Best Practices

Description

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.

Non-Compliant Code Examples

puts("Running")

BEGIN { puts("Beginning")}

my_str = begin
  a = "another "
  b = "block"
  a + b
end
puts(my_str)

Compliant Code Examples

puts("Beginning")

puts("Running")

my_str = begin
  a = "another "
  b = "block"
  a + b
end
puts(my_str)
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis