Cette page n'est pas encore disponible en français, sa traduction est en cours. Si vous avez des questions ou des retours sur notre projet de traduction actuel, n'hésitez pas à nous contacter.
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}.