This blog is part of our Ruby 2.5 series.
Ruby 2.4 added Hash#transform_values method to transform values of the hash.
In Ruby 2.5, a similar method Hash#transform_keys is added for transforming keys of the hash.
>> h = { name: "John", email: "[email protected]" }
=> {:name=>"John", :email=>"[email protected]"}
>> h.transform_keys { |k| k.to_s }
=> {"name"=>"John", "email"=>"[email protected]"}
The bang sibling of this method, Hash#transform_keys! is also added which
changes the hash in place.
These two methods are already present in Active Support from Rails and are natively supported in Ruby now.
Rails master is already supporting using the native methods if supported by the Ruby version.

Written byPrathamesh Sonpatki
Related blogs
- 01Ruby 2.5 added lazy proc allocation for block parametersMay 22, 2018 · Amit Choudhary
- 02Ruby 2.5 adds Thread.report_on_exception by defaultApril 18, 2018 · Vishal Telangre
- 03Ruby 2.5 adds Exception#full_message methodMarch 13, 2018 · Vishal Telangre
- 04Ruby 2.5 prints backtrace & error message in reverseMarch 7, 2018 · Vishal Telangre