Ruby

Ruby

Made by DeepSource

index_with can be used to create a hash from an enumerable RB-RL1059

Anti-pattern
Major
Autofix

For transforming an enumerable into a hash where the values are the original elements, Rails provides the index_with method.

Bad practice

# For example, while we could do this:
[1, 2, 3].each_with_object({}) { |el, h| h[el] = foo(el) }
[1, 2, 3].map { |el| [el, foo(el)] }.to_h
Hash[[1, 2, 3].collect { |el| [el, foo(el)] }]

Recommended

# the idiomatic way in a Rails app would be to do it like this:
[1, 2, 3].index_with { |el| foo(el) }