The rule “Prefer using hash key and value” encourages the use of the key? and value? methods when working with Ruby hashes, as opposed to the older has_key? and has_value? methods. These older methods are considered deprecated and may not be supported in future versions of Ruby.
This rule is important because it promotes the use of more modern, clean, and efficient Ruby syntax. By using key? and value?, your code will be more readable and maintainable, and you’ll avoid potential issues with deprecated methods in future Ruby versions.
To adhere to this rule, replace any instances of has_key? with key? and has_value? with value? in your Ruby code. For example, instead of hash.has_key?(:test), you should use hash.key?(:test). Similarly, replace hash.has_value?(value) with hash.value?(value). By following these practices, your Ruby code will be more modern, efficient, and future-proof.
Non-Compliant Code Examples
hash.has_key?(:test)hash.has_value?(value)
Compliant Code Examples
hash.key?(:test)hash.value?(value)
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- ruby-best-practices # Rules to enforce Ruby best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines