Ruby | Hash keep_if() function Last Updated : 07 Jan, 2020 Comments Improve Suggest changes Like Article Like Report Hash#keep_if() is a Hash class method which only keeps those key value pair that follows the block condition. Syntax: Hash.keep_if() Parameter: Hash values block - condition Return: key value pair that follows the block condition Example #1 : Ruby # Ruby code for Hash.keep_if() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, b:200} # declaring Hash value c = {a:100} # keep_if Value puts "Hash a keep_if form : #{a.keep_if{|key| key != 100}}\n\n" puts "Hash b keep_if form : #{b.keep_if{|key| key = 200}}\n\n" puts "Hash c keep_if form : #{c.keep_if{|key| key != 200}}\n\n" Output : Hash a keep_if form : {:a=>100, :b=>200} Hash b keep_if form : {:a=>100, :c=>300, :b=>200} Hash c keep_if form : {:a=>100} Example #2 : Ruby # Ruby code for Hash.keep_if() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {"a" => 100} # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} # keep_if Value puts "Hash a keep_if form : #{a.keep_if{|key| key != 100}}\n\n" puts "Hash b keep_if form : #{b.keep_if{|key| key = 200}}\n\n" puts "Hash c keep_if form : #{c.keep_if{|value| value != 200}}\n\n" Output : Hash a keep_if form : {"a"=>100, "b"=>200} Hash b keep_if form : {"a"=>100} Hash c keep_if form : {"a"=>100, "c"=>300, "b"=>200} Comment More infoAdvertise with us Next Article Ruby | Hash key() function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads Ruby | Hash key() function Hash#key() is a Hash class method which gives the key value corresponding to the value. If value doesn't exist then return nil. Syntax: Hash.key() Parameter: Hash values Return: key corresponding to the value nil - If value doesn't exist Example #1 : Ruby # Ruby code for Hash.key() method # declarin 2 min read Ruby | Hash key?() function Hash#key?() is a Hash class method which checks whether the key corresponding to the value is present or not. Syntax: Hash.key?() Parameter: Hash values Return: true - if key corresponding to the value is present otherwise return false Example #1 : Ruby # Ruby code for Hash.key?() method # declaring 2 min read Ruby | Regexp hash() function Regexp#force_encoding?() : force_encoding?() is a Regexp class method which returns the hash based on the text and options of this regular expression. Syntax: Regexp.hash() Parameter: Regexp values Return: the hash based on the text and options of this regular expression. Example #1 : Ruby # Ruby co 1 min read Ruby | Regexp hash() function Regexp#force_encoding?() : force_encoding?() is a Regexp class method which returns the hash based on the text and options of this regular expression. Syntax: Regexp.hash() Parameter: Regexp values Return: the hash based on the text and options of this regular expression. Example #1 : Ruby # Ruby co 1 min read Ruby | Hash keys() function Hash#keys() is a Hash class method which gives an array with all the keys present in the hash. Syntax: Hash.keys() Parameter: Hash values Return: array with all the keys present in the hash Example #1 : Ruby # Ruby code for Hash.keys() method # declaring Hash value a = {a:100, b:200} # declaring Has 2 min read Ruby | Hash merge! function Hash#merge!() : merge!() is a Hash class method which can add the content the given hash array to the other. Entries with duplicate keys are overwritten with the values from each other_hash successively if no block is given. Syntax: Hash.merge!() Parameter: Hash values Return: add the content the gi 2 min read Like