The rule “Prefer using reverse_each” is based on the principle of optimizing your Ruby code for better performance. In Ruby, there are two main methods for iterating over the elements of an array in reverse order: reverse.each and reverse_each. While the functionality of these two methods is the same, their performance characteristics are different.
The reverse.each method creates a new array that is the reverse of the original before performing the iteration. This can lead to unnecessary memory usage, especially for large arrays, and slow down the performance of your code.
On the other hand, the reverse_each method does not create a new array. Instead, it starts from the end of the original array and works its way to the beginning, saving memory and enhancing performance.
Therefore, to adhere to good coding practices and ensure optimal performance, it is recommended to use reverse_each instead of reverse.each when you need to iterate over the elements of an array in reverse order.
Non-Compliant Code Examples
array.reverse.each{}["foo","bar"].reverse.each{}
Compliant Code Examples
array.reverse_each{}["foo","bar"].reverse_each{}
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