---
title: "Ruby 2.5 adds Hash#transform_keys method"
description:
  "Neat way to transform keys of the Hash, now natively supported by Ruby"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-5-adds-hash-transform_keys-method"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-5-adds-hash-transform_keys-method.md"
---

# Ruby 2.5 adds Hash#transform_keys method

Neat way to transform keys of the Hash, now natively supported by Ruby

- Author: Prathamesh Sonpatki
- Published: January 9, 2018
- Categories: Ruby 2.5, Ruby

Ruby 2.4 added
[Hash#transform_values](https://bigbinary.com/blog/ruby-2-4-added-hash-transform-values-and-its-destructive-version-from-active-support)
method to transform values of the hash.

In Ruby 2.5, a similar method
[Hash#transform_keys](https://bugs.ruby-lang.org/issues/13583) is added for
transforming keys of the hash.

```ruby
>> h = { name: "John", email: "john@example.com" }
=> {:name=>"John", :email=>"john@example.com"}

>> h.transform_keys { |k| k.to_s }
=> {"name"=>"John", "email"=>"john@example.com"}
```

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](http://guides.rubyonrails.org/active_support_core_extensions.html#transform-keys-and-transform-keys-bang)
and are natively supported in Ruby now.

Rails master is already supporting
[using the native methods](https://github.com/rails/rails/commit/f213e926892) if
supported by the Ruby version.

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-5-adds-hash-transform_keys-method)
