KEMBAR78
Style/MapToHash: Ambiguous/Unwarranted suggestion on map(block).to_h(block) · Issue #14416 · rubocop/rubocop · GitHub
Skip to content

Style/MapToHash: Ambiguous/Unwarranted suggestion on map(block).to_h(block) #14416

@forthrin

Description

@forthrin

Seems like an obvious issue that has been considered during implementation of this cop, but couldn't find any duplicates searching for "map to_h", so reporting just in case.

Steps to reproduce the problem

[1, 2, 3].map { it * 10 }.to_h do
  [it, it * 100]
end.then { pp it }

Actual behavior

Says Style/MapToHash: Pass a block to to_h instead of calling map.to_h.

Replacing map with to_h will obviously break the code, as to_h belongs to its following block

The message could implicitly hint at merging the code like so, but this requires some lenience on the reader's part.

[1, 2, 3].to_h do
  it *= 10
  [it, it * 100]
end.then { pp it }

Expected behavior

A) Whitelist it (preferably)
B) Suggest something like "merge the blocks", but original code might be perfectly acceptable for clarity (so rather not)

RuboCop version

ruby   : 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [arm64-darwin24]
rubocop: git 2025-08-04 12:08:56 eb470cef9
System : macOS 15.5 (24F74)
Model  : MacBookAir10,1

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions