Home » Ruby programming

Hash.flatten Method with Example in Ruby

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

Hash.flatten Method

In this article, we will study about Hash.flatten 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 Ruby library especially for Hash class. This method works in a way that it returns an array object after flattening the whole hash object. This means that each key-value pair will be converted into an array object or you can say that will become an individual element of the Array object. This method is a non-destructive method which means that the changes created by this method would not affect the actual hash instance.

Syntax:

    Hash_object.flatten
    or
    Hash_object.flatten(level)

Argument(s) required:

This method only accepts one argument and that argument is nothing but the level of flattening you want to do with the hash object.

Example 1:

=begin
  Ruby program to demonstrate flatten method
=end	

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

puts "Hash.flatten implementation"
ary = hash1.flatten
puts "Hash object after flatten: #{ary}"
puts "Self hash object : #{hash1}"

Output

Hash.flatten implementation
Hash object after flatten: ["color", "Black", "object", ["car", "phone"], "love", ["mom", "friends"], "fruit", "Kiwi", "vege", "potato"]
Self hash object : {"color"=>"Black", "object"=>["car", "phone"], "love"=>["mom", "friends"], "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

In the above code, you can observe that we are flattening the hash object with the help of the Hash.flatten method. The first level flattening has been done in which all the hash elements are now a part of an array. You can see that this method is not creating any impact upon the actual hash because this method is one of the examples of non-destructive methods.

Example 2:

=begin
  Ruby program to demonstrate flatten method
=end	

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

puts "Hash.flatten implementation"

puts "Enter the level of flatten"
lvl = gets.chomp.to_i

ary = hash1.flatten(lvl)

puts "Hash object after flatten: #{ary}"
puts "Self hash object : #{hash1}"

Output

Hash.flatten implementation
Enter the level of flatten
 2
Hash object after flatten: ["color", "Black", "object", "car", "phone", "love", "mom", "friends", "fruit", "Kiwi", "vege", "potato"]
Self hash object : {"color"=>"Black", "object"=>["car", "phone"], "love"=>["mom", "friends"], "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

In the above code, you can observe that we are flattening the hash object with the help of the Hash.flatten method. The second level flattening has been done in which all the hash elements are now a part of an array. You can see that this method is not creating any impact upon the actual hash because this method is one of the examples of non-destructive methods.



Comments and Discussions!

Load comments ↻





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