Range#cover?
instead of Range#include?
RB-PR1014Range#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:
It is more efficient to replace Hash#merge!
with Hash#[]=
.
case
..when
can be modified for performance RB-PR1002Reordering 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.
casecmp
RB-PR1003Case-insensitive string comparison can be done more efficiently using casecmp
.
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.