Ruby

Ruby

Made by DeepSource

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

Performance
Major

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:

('a'..'z').include?('b')

can be replaced by

('a'..'z').cover?('b')

The raised issue can be ignored in certain cases, like this, where Range#cover? may not provide the desired result.

('a'..'z').cover?('yellow')