The Ruby static analysis rule “Use Kernel#loop instead of while/until” focuses on promoting the use of Kernel#loop for infinite loops rather than while true or until false. This is because Kernel#loop is more idiomatic to Ruby, and it communicates the intent of an infinite loop more clearly. This rule helps to maintain readability, which is crucial in large codebases where understanding the flow and function of the code is important.
The while true or until false expressions can be misleading as they suggest a condition that might change, even though they are used to create infinite loops. Using Kernel#loop eliminates this ambiguity, making the code easier to understand.
To adhere to this rule, replace any while true or until false loops with Kernel#loop. The body of the loop remains the same. This small change can significantly improve the clarity of the code, making it more understandable for other developers who might work on the same codebase.
Non-Compliant Code Examples
whiletruedo_somethingenduntilfalsedo_somethingend
Compliant Code Examples
loopdodo_somethingend
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