Prefer using hash syntax for enums

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

Metadata

ID: rails-best-practices/enums

Language: Ruby

Severity: Notice

Category: Best Practices

Description

When defining enums in Ruby, it is better to use the hash syntax rather than the array syntax. This is because the hash syntax is more explicit and less error-prone. With the hash syntax, the mapping between the enum keys and their underlying integer values is clearly defined.

This rule is important because when using the array syntax, the integer values are implicitly assigned based on the order of the keys in the array. This can lead to subtle bugs if the order of the keys in the array is changed. For example, if a new key is inserted in the middle of the array, the integer values of the keys that come after it will be shifted, which can cause existing data to be misinterpreted.

To avoid violating this rule, always use the hash syntax when defining enums in Ruby. Specify the integer value for each key explicitly. For example, instead of enum status: [:pending, :completed], write enum status: {pending: 0, completed: 1}.

Non-Compliant Code Examples

class Transaction < ApplicationRecord
  enum type: %i[income expense]
  
  enum status: [:pending, :completed]
end

Compliant Code Examples

class Transaction < ApplicationRecord
  enum type: {
    income: 0,
    expense: 1
  }
  
  enum status: {
    pending: 0,
    completed: 1
  }
end
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

PREVIEWING: rtrieu/product-analytics-ui-changes