Home » Ruby programming

Hash.transform_values Method with Example in Ruby

Ruby Hash.transform_values Method: Here, we are going to learn about the Hash.transform_values Method with examples in Ruby programming language.
Submitted by Hrithik Chandra Prasad, on March 14, 2020

Hash.transform_values Method

In this article, we will study about Hash.transform_values Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method with the help of its syntax and program code in the rest of the content.

Method description:

This method is a public instance method that is defined in the ruby library especially for Hash class. This method works in a way that it runs the provided block at least once for each hash value and returns a new hash. An enumerator will be returned if you are not providing any block along with the method at the time of its invocation.

This method does not bring change in the actual hash because this method belongs to the category of non-destructive methods.

Syntax:

    Hash_object.transform_values
    or
    Hash_object.transform_values{|value| block}

Argument(s) required:

This method does not accept any parameter instead you can provide a block for a better implementation.

Example 1:

=begin
  Ruby program to demonstrate transform_values method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash transform_values implementation"

hsh = hash1.transform_values{|val| val+val}

puts  "Value hash is #{hsh}"

puts "Self hash object : #{hash1}"

Output

Hash transform_values implementation
Value hash is {"color"=>"BlackBlack", "object"=>"carcar", "love"=>"friendsfriends", "fruit"=>"KiwiKiwi", "vege"=>"potatopotato"}
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

In the above code, you can observe that we are updating the values of the hash object with the help of the Hash.transform_values() method. You can see that all the values are updated by their names. This method is not creating changes in the actual hash object because this method is one of the examples of non-destructive methods.

Example 2:

=begin
  Ruby program to demonstrate transform_values method
=end	

hash1={"color"=>"Black","object"=>"car","love"=>"friends","fruit"=>"Kiwi","vege"=>"potato"}

puts "Hash transform_values implementation"

hsh = hash1.transform_values

puts  "Value hash is #{hsh}"

puts "Self hash object : #{hash1}"

Output

Hash transform_values implementation
Value hash is #<Enumerator:0x0000559e6dc742f8>
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

In the above code, you can observe that when we are invoking the method without passing any block then the method is returning an enumerator.



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.