---
title: "Ruby 2.4 Hash#transform_values & destructive version"
description:
  "In Ruby 2.4, Hash#transform_values and Hash#transform_values! can be used to
  transform the values after executing passed block for each value"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-4-added-hash-transform-values-and-its-destructive-version-from-active-support"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-4-added-hash-transform-values-and-its-destructive-version-from-active-support.md"
---

# Ruby 2.4 Hash#transform_values & destructive version

In Ruby 2.4, Hash#transform_values and Hash#transform_values! can be used to
transform the values after executing passed block for each value

- Author: Sushant Mittal
- Published: June 14, 2017
- Categories: Ruby 2.4, Ruby

It is a common use case to transform the values of a hash.

```ruby

{ a: 1, b: 2, c: 3 } => { a: 2, b: 4, c: 6 }

{ a: "B", c: "D", e: "F" } => { a: "b", c: "d", e: "f" }

```

We can transform the values of a hash destructively (i.e. modify the original
hash with new values) or non-destructively (i.e. return a new hash instead of
modifying the original hash).

Prior to Ruby 2.4, we need to use following code to transform the values of a
hash.

```ruby

# Ruby 2.3 Non-destructive version

> hash = { a: 1, b: 2, c: 3 }
 #=> {:a=>1, :b=>2, :c=>3}

> hash.inject({}) { |h, (k, v)| h[k] = v * 2; h }
 #=> {:a=>2, :b=>4, :c=>6}

> hash
 #=> {:a=>1, :b=>2, :c=>3}

> hash = { a: "B", c: "D", e: "F" }
 #=> {:a=>"B", :c=>"D", :e=>"F"}

> hash.inject({}) { |h, (k, v)| h[k] = v.downcase; h }
 #=> {:a=>"b", :c=>"d", :e=>"f"}

> hash
 #=> {:a=>"B", :c=>"D", :e=>"F"}

```

```ruby

# Ruby 2.3 Destructive version

> hash = { a: 1, b: 2, c: 3 }
 #=> {:a=>1, :b=>2, :c=>3}

> hash.each { |k, v| hash[k] = v * 2 }
 #=> {:a=>2, :b=>4, :c=>6}

> hash
 #=> {:a=>2, :b=>4, :c=>6}

> hash = { a: "B", c: "D", e: "F" }
 #=> {:a=>"B", :c=>"D", :e=>"F"}

> hash.each { |k, v| hash[k] = v.downcase }
 #=> {:a=>"b", :c=>"d", :e=>"f"}

> hash
 #=> {:a=>"b", :c=>"d", :e=>"f"}

```

## transform_values and transform_values! from Active Support

Active Support has already implemented handy methods
[Hash#transform_values and Hash#transform_values!](https://github.com/rails/rails/commit/b2cf8b251aac39c1e3ce71bc1de34a2ce5ef52b1)
to transform hash values.

Now, Ruby 2.4 has also implemented
[Hash#map_v and Hash#map_v!](https://github.com/ruby/ruby/commit/ea5184b939ec3dbdcbd7013da350b8dcb6ca6107)
and then renamed to
[Hash#transform_values and Hash#transform_values!](https://github.com/ruby/ruby/commit/eaa0a27f6149a9afa2b29729307ff9cc7b0bc95f)
for the same purpose.

```ruby

# Ruby 2.4 Non-destructive version

> hash = { a: 1, b: 2, c: 3 }
 #=> {:a=>1, :b=>2, :c=>3}

> hash.transform_values { |v| v * 2 }
 #=> {:a=>2, :b=>4, :c=>6}

> hash
 #=> {:a=>1, :b=>2, :c=>3}

> hash = { a: "B", c: "D", e: "F" }
 #=> {:a=>"B", :c=>"D", :e=>"F"}

> hash.transform_values(&:downcase)
 #=> {:a=>"b", :c=>"d", :e=>"f"}

> hash
 #=> {:a=>"B", :c=>"D", :e=>"F"}

```

```ruby

# Ruby 2.4 Destructive version

> hash = { a: 1, b: 2, c: 3 }
 #=> {:a=>1, :b=>2, :c=>3}

> hash.transform_values! { |v| v * 2 }
 #=> {:a=>2, :b=>4, :c=>6}

> hash
 #=> {:a=>2, :b=>4, :c=>6}

> hash = { a: "B", c: "D", e: "F" }
 #=> {:a=>"B", :c=>"D", :e=>"F"}

> hash.transform_values!(&:downcase)
 #=> {:a=>"b", :c=>"d", :e=>"f"}

> hash
 #=> {:a=>"b", :c=>"d", :e=>"f"}

```

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-4-added-hash-transform-values-and-its-destructive-version-from-active-support)
