class RuboCop::Cop::Performance::InefficientHashSearch
h = { a: 1, b: 2 }; h.value?(nil)
{ a: 1, b: 2 }.has_value?(‘garbage’)
{ a: 1, b: 2 }.value?(2)
# good
h = { a: 1, b: 2 }; h.values.include?(nil)
{ a: 1, b: 2 }.values.include?(‘garbage’)
{ a: 1, b: 2 }.values.include?(2)
# bad
h = { a: 1, b: 2 }; h.key?(100)
{ a: 1, b: 2 }.has_key?(:z)
{ a: 1, b: 2 }.key?(:a)
# good
h = { a: 1, b: 2 }; h.keys.include?(100)
{ a: 1, b: 2 }.keys.include?(:z)
{ a: 1, b: 2 }.keys.include?(:a)
# bad
@example
This cop is unsafe because it can’t tell whether the receiver is a hash object.
@safety
allocates a new array while using ‘value?` does not.
both perform an O(n) search through all of the values, calling `values`
`Hash#values.include?` is less efficient than `Hash#value?`. While they
performs a faster O(1) search for the key.
through that array, while `Hash#key?` does not allocate any array and
the former allocates a new array and then performs an O(n) search
`Hash#keys.include?` is less efficient than `Hash#key?` because
hashes.
Checks for inefficient searching of keys and values within
def autocorrect_argument(node)
def autocorrect_argument(node) node.arguments.first.source end
def autocorrect_hash_expression(node)
def autocorrect_hash_expression(node) node.receiver.receiver.source end
def autocorrect_method(node)
def autocorrect_method(node) case current_method(node) when :keys then use_long_method ? 'has_key?' : 'key?' when :values then use_long_method ? 'has_value?' : 'value?' end end
def current_method(node)
def current_method(node) node.receiver.method_name end
def message(node)
def message(node) "Use `##{autocorrect_method(node)}` instead of `##{current_method(node)}.include?`." end
def on_send(node)
def on_send(node) inefficient_include?(node) do |receiver| return if receiver.nil? message = message(node) add_offense(node, message: message) do |corrector| # Replace `keys.include?` or `values.include?` with the appropriate # `key?`/`value?` method. corrector.replace( node.loc.expression, "#{autocorrect_hash_expression(node)}.#{autocorrect_method(node)}(#{autocorrect_argument(node)})" ) end end end
def use_long_method
def use_long_method preferred_config = config.for_all_cops['Style/PreferredHashMethods'] preferred_config && preferred_config['EnforcedStyle'] == 'long' && preferred_config['Enabled'] end