Project

General

Profile

Actions

Feature #9635

closed

Map a hash directly to a hash

Added by lolmaus (Andrey Mikhaylov) over 10 years ago. Updated about 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:61462]

Description

There should be a way to directly map a hash to a hash.

The current way to do it is not very graceful:

{foo: 'foo', bar: 'bar'}.map { |key, value| [key.upcase, value.reverse] }.to_h     # => { :FOO => 'oof', :BAR => 'rab' }

I think there should be a simpler and more natural way to do that. Something like this:

{foo: 'foo', bar: 'bar'}.hashy_map { |key, value| key.upcase => value.reverse }    # => { :FOO => 'oof', :BAR => 'rab' }

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupportClosedmatz (Yukihiro Matsumoto)Actions

Updated by lolmaus (Andrey Mikhaylov) over 10 years ago

Ok, what i suggest is not a valid Ruby syntax. It should be

.hashy_map { |key, value| {key => value} }

which is hardly sweeter than

.map { |key, value| [key, value] }.to_h

and also goes against common practice of returning multiple results in an array.

@apeiros (Stefan Rusterholz) from irc://irc.freenode.net/ruby/ has cleared this out to me and suggested this very compact and efficient implementation: https://gist.github.com/apeiros/9527275

Actions #3

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

  • Related to Feature #12512: Import Hash#transform_values and its destructive version from ActiveSupport added
Actions #4

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0