Feature #17315
open
Added by dsisnero (Dominic Sisneros) about 4 years ago.
Updated about 4 years ago.
Description
Add new methods to transform
or transform!
both the keys and the values of a hash.
#7793
h = {'name' => 'dominic ', 'email' => 'dominic.mail.com '}
h.transform!{|k,v| [k.to_sym, v.strip]}
h # => {name: 'dominic', email: 'dominic.mail.com'}
- Status changed from Open to Closed
You'll be happy to hear that this is exactly what to_h
does already.
- Status changed from Closed to Open
I'll reopen this, as you are asking for a mutating version of to_h
.
- Description updated (diff)
- Description updated (diff)
marcandre (Marc-Andre Lafortune) wrote in #note-2:
I'll reopen this, as you are asking for a mutating version of to_h
.
It might be strange to have transform!
as the destructive counterpart to to_h
. Perhaps, the naming should be Hash#to_h!
? Wait. That looks even more strange.
I didn't suggest anything, as I don't see a good name, and I don't see the appeal of this method:
- it can not be more efficient than
update
+ to_h
- there's not a frequent usecase that I know of
- seems like avoidable mutation
-
to_h!
would be strange (conversion + mutation...?)
to_h works for me even though it is unintuitive a name for monadic mapping a hash. I wish we had a common name function name in Enumerable for monadic mapping that would return the same Class that we map over.
Also available in: Atom
PDF
Like0
Like0Like0Like0Like0Like0Like0Like0