Ruby | Hash compact!() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report compact! () is a Hash class method which returns the Hash after removing all the 'nil' value elements (if any) from the Hash. If there are no nil values in the Hash it returns back the nil value. Syntax: Hash.compact!() Parameter: Hash to remove the 'nil' value from. Return: removes all the nil values from the Hash. nil - if there is no nil value in the Hash Example #1 : Ruby # Ruby code for compact!() method # showing how to remove nil values # declaring Hash value a = {a:100, b:nil} # declaring Hash value b = {a:100, c:nil, b:200} # declaring Hash value c = {a:100} # removing nil value from Hash puts "removing nil value : #{a.compact!}\n\n" # removing nil value from Hash puts "removing nil value : #{b.compact!}\n\n" # removing nil value from Hash puts "removing nil value : #{c.compact!}\n\n" Output : removing nil value : {a:100} removing nil value : {a:100, b:200} removing nil value : {} Example #2: Ruby # Ruby code for compact!() method # showing how to remove nil values # declaring Hash value a = { "a" => nil, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"a" => 100, "c" => nil, "b" => 200} # removing nil value from Hash puts "removing nil value : #{a.compact!}\n\n" # removing nil value from Hash puts "removing nil value : #{b.compact!}\n\n" # removing nil value from Hash puts "removing nil value : #{c.compact!}\n\n" Output : removing nil value : {b:200} removing nil value : removing nil value : {a:100, b:200} Comment More infoAdvertise with us Next Article Ruby | Hash clear() function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads Ruby | Hash compact() function compact () is a Hash class method which returns the Hash after removing all the 'nil' value elements (if any) from the Hash. Syntax: Hash.compact() Parameter: Hash to remove the 'nil' value from. Return: removes all the nil values from the Hash. Example #1: Ruby # Ruby code for compact() method # sh 2 min read Ruby | Hash empty? function Hash#empty?() is a Hash class method which checks whether the Hash array has any key-value pair. Syntax: Hash.empty?()Parameter: Hash valuesReturn: true - if no key value pair otherwise return false Example #1 :  Ruby # Ruby code for Hash.empty?() method # declaring Hash value a = {a:100, b:200} # 1 min read Ruby | Hash each() function Hash#each() is a Hash class method which finds the nested value which calls block once for each key in hash by passing the key-value pair as parameters. Syntax: Hash.each() Parameter: Hash values Return: calls block once for each key in hash otherwise Enumerator if no argument is passed. Example #1 2 min read Ruby | Hash assoc() function assoc() is an Hash class method which searches an element through the Hash. Syntax: Hash.assoc() Parameter: Hashs for finding elements. Return: searches an element through the Hash Example #1: Ruby # Ruby code for assoc() method # declaring Hash value a = { "a" => 100, "b" = 1 min read Ruby | Hash clear() function clear() is an Hash class method which removes all the Hash elements from it. Syntax: Hash.clear() Parameter: Hashs to clear off Return: Hash with all elements cleared Example #1: Ruby # Ruby code for clear() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b 1 min read Ruby | Hash any?() function any?() is a Hash class method which checks for the presence of a pattern and passes each element of the collection to the given block. Syntax: Hash.any?() Parameter: Hash for testing Return: true - if the block ever returns a value other than false or nil otherwise return false Example #1: Ruby # Ru 1 min read Like