Class: RuboCop::Cop::Rails::IndexWith

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector, TargetRailsVersion
Includes:
IndexMethod
Defined in:
lib/rubocop/cop/rails/index_with.rb

Overview

Looks for uses of ‘each_with_object({}) { … }`, `map { … }.to_h`, and `Hash[map { … }]` that are transforming an enumerable into a hash where the keys are the original elements. Rails provides the `index_with` method for this purpose.

Examples:

# bad
[1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }
[1, 2, 3].to_h { |el| [el, foo(el)] }
[1, 2, 3].map { |el| [el, foo(el)] }.to_h
Hash[[1, 2, 3].collect { |el| [el, foo(el)] }]

# good
[1, 2, 3].index_with { |el| foo(el) }

Constant Summary

Constants included from TargetRailsVersion

TargetRailsVersion::TARGET_GEM_NAME, TargetRailsVersion::USES_REQUIRES_GEM_API

Constants included from IndexMethod

IndexMethod::RESTRICT_ON_SEND

Method Summary

Methods included from TargetRailsVersion

minimum_target_rails_version, support_target_rails_version?

Methods included from IndexMethod

#on_block, #on_csend, #on_send