Use new syntax when keys are symbols

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: ruby-best-practices/hash-literals

Language: Ruby

Severity: Notice

Category: Best Practices

Description

The rule “Use new syntax when keys are symbols” is a coding standard in modern Ruby development. It encourages the use of the new hash syntax, introduced in Ruby 1.9, where symbols are used as keys. The old hash rocket syntax (:symbol => value) is replaced with the more elegant and succinct symbol: value syntax.

This rule is important as it promotes a cleaner, more readable code. The new syntax is more concise and less cluttered, making it easier to understand the structure and purpose of the hash. This is particularly beneficial in large codebases or when hashes are nested or complex.

To adhere to this rule, always use the new syntax when defining hashes where keys are symbols. Instead of defining a hash with :symbol => value, use symbol: value. This approach will not only make your code more readable but also ensure consistency across your codebase.

Non-Compliant Code Examples

values = { :foo => 42, :bar => 99, :baz => 123 }

Compliant Code Examples

values = { foo: 42, bar: 99, baz: 123 }
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