Ruby

Ruby

By DeepSource

Use Range#cover? instead of Range#include? RB-PR1014
Performance

Range#include? iterates over each item in a Range to see if a specified item is there. In contrast, Range#cover? simply compares the target item with the beginning and end points of the Range. In a great majority of cases, this is what is wanted. For example:

Hash merging can be replaced by hash key assignment RB-PR1017
Performance
Autofix

It is more efficient to replace Hash#merge! with Hash#[]=.

case..when can be modified for performance RB-PR1002
Performance

Reordering when conditions with a splat to the end of the when branches can improve performance. Ruby has to allocate memory for the splat expansion every time that the case-when statement is run. Since Ruby does not support fall through inside of case-when, like some other languages do, the order of the when branches should not matter. By placing any splat expansions at the end of the list of when branches we will reduce the number of times that memory has to be allocated for the expansion. The exception to this is if multiple when conditions can be true for any given condition. A likely scenario for this is defining a higher level when condition to override a condition that is inside of the splat expansion.

Case-insensitive comparison of strings can use casecmp RB-PR1003
Performance
Autofix

Case-insensitive string comparison can be done more efficiently using casecmp.

Replace methods on array with mutations RB-PR1004
Performance

Methods compact, flatten and map generate a new intermediate array that is discarded. It is faster to mutate the array when we know it's safe.