class RuboCop::Cop::Style::HashTransformKeys
{a: 1, b: 2}.transform_keys { |k| k.to_s }
{a: 1, b: 2}.transform_keys { |k| foo(k) }
# good
{a: 1, b: 2}.to_h { |k, v| [k.to_s, v] }
{a: 1, b: 2}.map { |k, v| [k.to_s, v] }.to_h
Hash[{a: 1, b: 2}.collect { |k, v| [foo(k), v] }]
{a: 1, b: 2}.each_with_object({}) { |(k, v), h| h = v }
# bad
@example
(‘transform_keys` was added in Ruby 2.5.)
This cop should only be enabled on Ruby version 2.5 or newer
`[[k1, v1], [k2, v2], …]`
of key-value-like pairs that isn’t actually a hash, e.g.:
This can produce false positives if we are transforming an enumerable
call to ‘transform_keys` instead.
transforming the keys of a hash, and tries to use a simpler & faster
`_.map {…}.to_h`, and `Hash[_.map {…}]` that are actually just
This cop looks for uses of `_.each_with_object({}) {…}`,
def extract_captures(match)
def extract_captures(match) key_argname, key_body_expr, val_body_expr = *match Captures.new(key_argname, key_body_expr, val_body_expr) end
def new_method_name
def new_method_name 'transform_keys' end