Feature #8951

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

Added by Behrang Saeedzadeh 7 months ago. Updated 5 months ago.

[ruby-core:57367]
Status:Open
Priority:Normal
Assignee:-
Category:-
Target version:-

Description

History

#1 Updated by Fuad Saud 7 months 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/

#2 Updated by Jamon Holmgren 7 months ago

How about:

Hash#graph

or

Hash#rehash

#3 Updated by Fuad Saud 7 months 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/

#4 Updated by Boris Stitnicky 7 months ago

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

Hash#withkeys
Hash#with
values
Hash#modify

They work like this:

h = { a: 1, b: 2 }
h.withkeys do |k| k.tos end #=> { "a"=>1, "b"=>2 }
h.withvalues do |v| v.tos 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#withkeys!, Hash#withvalues!, 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 :-)

#5 Updated by Behrang Saeedzadeh 6 months 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

#6 Updated by Fuad Saud 5 months ago

Hash#transform might also be a good name.

Also available in: Atom PDF