Use
Range#cover?
instead of Range#include?
.64end
65
66# Raises "Use `Range#cover?` instead of `Range#include?`"
67(1..9).include?(5)68
69my_hash = {}
70# Raises "Hash merging can be replaced by hash key assignment"
Description
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')