Home » Ruby programming

Hash.keep_if Method with Example in Ruby

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

Hash.keep_if Method

In this article, we will study about Hash.keep_if 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 removes every key-value pair from the hash object for which the block has evaluated to be false. If you are not providing any block, then this method will return an enumerator.

This method is one of the examples of destructive methods where changes created by the methods are permanent or non-temporary.

Syntax:

    Hash_object.keep_if
    or
    Hash_object.keep_if{|key,value| block}

Argument(s) required:

This method does not require any argument. You will need to pass a block with the method for its better implementation.

Example 1:

=begin
  Ruby program to demonstrate keep_if method
=end	

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

puts "Hash keep_if implementation"

puts "Enter the key you want to keep:"
ky = gets.chomp

puts "Hash after keep_if :#{hash1.keep_if{|key,value| key==ky}}"

puts "Self hash object : #{hash1}"

Output

Hash keep_if implementation
Enter the key you want to keep:
 color
Hash after keep_if :{"color"=>"Black"}
Self hash object : {"color"=>"Black"}

Explanation:

In the above code, you can observe that we are deleting elements from the hash object with the help of Hash.keep_if method. You can see that the method is deleting all the elements for which the method has returned false. This method is one of the examples of destructive methods because it is creating changes in the self hash objects.

Example 2:

=begin
  Ruby program to demonstrate keep_if method
=end	

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

puts "Hash keep_if implementation"

puts "Hash after keep_if :#{hash1.keep_if}"
puts "Self hash object : #{hash1}"

Output

Hash keep_if implementation
Hash after keep_if :#<Enumerator:0x000055f1998a3b90>
Self hash object : {"color"=>"Black", "object"=>"car", "love"=>"friends", "fruit"=>"Kiwi", "vege"=>"potato"}

Explanation:

In the above code, you can observe that this method returns an enumerator when called without providing any block at the time of invocation.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.