Mapping hash keys and values is tricky in Ruby. Mapping a hash returns an
array, but we want a hash. Why bother coercing it every time?
To simplify that a bit, AS introduced Hash#transform_keys. I have noticed
Rails source base still uses a few tricks to map values of hashes in
different places. Additionally, applications built on top of Rails might
experience the need to map hash values as well.
That said, I propose to extend Hash to allow this, in manner similar to
#transform_keys:
{ a: 1, b: 2 }.transform_keys { |key| :"_#{key}" } # => { :_a => 1, :_b =>
2 }
{ a: 1, b: 2 }.transform_values { |val| val ** 2 } # => { :a => 1, :b => 4 }
Yay or nay?
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/rubyonrails-core/-/jE9nOBXywWkJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.