Project

General

Profile

Actions

Feature #8951

closed

Please add a hash-to-hash alternative of the map method to Hash

Added by behrangsa (Behrang Saeedzadeh) about 11 years ago. Updated about 3 years ago.

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

Description


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 fuadksd (Fuad Saud) about 11 years ago

Wow, I missed this a couple of days ago. Totally supported.

--
Fuad Saud
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Wednesday, September 25, 2013 at 1:05 AM, behrangsa (Behrang Saeedzadeh) wrote:

Issue #8951 has been reported by behrangsa (Behrang Saeedzadeh).


Feature #8951: Please add a hash-to-hash alternative of the map method to Hash
https://bugs.ruby-lang.org/issues/8951

Author: behrangsa (Behrang Saeedzadeh)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:

Please see this thread: https://twitter.com/behrangsa/status/382670159086776323

--
http://bugs.ruby-lang.org/

Updated by jamonholmgren (Jamon Holmgren) about 11 years ago

How about:

Hash#graph

or

Hash#rehash

Updated by fuadksd (Fuad Saud) about 11 years ago

#rehash is taken: http://www.ruby-doc.org/core-2.0.0/Hash.html#method-i-rehash

I was thinking about 'projection' or something, but everything feels just like an alternative to 'map' (#map, in my opinion, should return a hash).

--
Fuad Saud
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Wednesday, September 25, 2013 at 1:36 AM, jamonholmgren (Jamon Holmgren) wrote:

Issue #8951 has been updated by jamonholmgren (Jamon Holmgren).

How about:

Hash#graph

or

Hash#rehash


Feature #8951: Please add a hash-to-hash alternative of the map method to Hash
https://bugs.ruby-lang.org/issues/8951#change-41971

Author: behrangsa (Behrang Saeedzadeh)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:

Please see this thread: https://twitter.com/behrangsa/status/382670159086776323

--
http://bugs.ruby-lang.org/

Updated by Anonymous about 11 years ago

In my private library, I have 3 map-like methods defined on hash:

Hash#with_keys
Hash#with_values
Hash#modify

They work like this:

h = { a: 1, b: 2 }
h.with_keys do |k| k.to_s end #=> { "a"=>1, "b"=>2 }
h.with_values do |v| v.to_s end #=> { a: "1", b: "2" }
h.modify do |k, v| [k, "#{k}#{v}"] end #=> { a: "a1", b: "b2" }

plus their ! versions that modify the hash in place:

Hash#with_keys!, Hash#with_values!, Hash#modify!

But, this much being said, I am not sure whether this feature creep is such a good thing. Part of the fun in Ruby is that you are inventing your own libraries. If everything simple is already done in advance, it will be more "work" memorizing and less "fun" inventing :-))) Just randomly saying whatever comes to my mind, don't take me too seriously :-)

Updated by behrangsa (Behrang Saeedzadeh) about 11 years ago

How about having a construct similar to map.with_index? For example map.to_has or map.hashify:

hash = { ... }
hash.map.to_hash do |e|
 convert(e)
end

or

hash = { ... }
hash.map.hashify do |e|
 convert(e)
end

Updated by fuadksd (Fuad Saud) about 11 years ago

Hash#transform might also be a good name.

Updated by Ajedi32 (Ajedi32 W) over 10 years ago

I believe this is a duplicate of #6669

Actions #8

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 #9

Updated by jeremyevans0 (Jeremy Evans) about 3 years ago

  • Status changed from Open to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0