class RuboCop::Cop::Style::HashTransformValues
{a: 1, b: 2}.transform_values { |v| v * v }
{a: 1, b: 2}.transform_values { |v| foo(v) }
# good
{a: 1, b: 2}.to_h { |k, v| [k, v * v] }
{a: 1, b: 2}.map { |k, v| [k, v * v] }.to_h
Hash[{a: 1, b: 2}.collect { |k, v| [k, foo(v)] }]
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h = foo(v) }
# bad
@example
a hash, e.g.: ‘[[k1, v1], [k2, v2], …]`
transforming an enumerable of key-value-like pairs that isn’t actually
This cop is unsafe, as it can produce false positives if we are
@safety
call to ‘transform_values` instead.
transforming the values of a hash, and tries to use a simpler & faster
`+_.map {…}.to_h+`, and `+Hash[_.map {…}]+` that are actually just
Looks for uses of `+_.each_with_object({}) {…}+`,
def extract_captures(match)
def extract_captures(match) val_argname, key_body_expr, val_body_expr = *match Captures.new(val_argname, val_body_expr, key_body_expr) end
def new_method_name
def new_method_name 'transform_values' end