Ruby | Hash empty? function Last Updated : 15 Apr, 2021 Comments Improve Suggest changes Like Article Like Report 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} # declaring Hash value b = {a:100, c:300, b:200} # declaring Hash value c = {a:100} # empty? Value puts "Hash a empty? form : #{a.empty?()}\n\n" puts "Hash b empty? form : #{b.empty?()}\n\n" puts "Hash c empty? form : #{c.empty?()}\n\n" Output : Hash a empty? form : false Hash b empty? form : false Hash c empty? form : false Example #2 : Ruby # Ruby code for Hash.empty?() method # declaring Hash value a = { "a" => 100, "b" => 200 } # declaring Hash value b = {} # declaring Hash value c = {"a" => 100, "c" => 300, "b" => 200} # emoty? Value puts "Hash a empty? form : #{a.empty?()}\n\n" puts "Hash b empty? form : #{b.empty?()}\n\n" puts "Hash c empty? form : #{c.empty?()}\n\n" Output : Hash a empty? form : false Hash b empty? form : true Hash c empty? form : false Comment More infoAdvertise with us Next Article Ruby | Hash eql? 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. 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 valu 2 min read 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 fetch function Hash#fetch() is a Hash class method which returns a value from the hash for the given key. With no other arguments, it will raise a KeyError exception. Syntax: Hash.fetch() Parameter: Hash values Return: value from the hash for the given key Example #1 : Ruby # Ruby code for Hash.fetch() method # de 2 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 Ruby | Hash eql? function Hash#eql?() is a Hash class method which checks whether the two Hash arrays are equal or not. Syntax: Hash.eql?() Parameter: Hash values Return: true - if two hash arrays are equal otherwise return false Example #1 : Ruby # Ruby code for Hash.eql?() method # declaring Hash value a = {a:100, b:200} # 2 min read Ruby | Hash delete() function delete() is an Hash class method which deletes the key-value pair and returns the value from hash whose key is equal to key. Syntax: Hash.delete() Parameter: Hash array Return: value from hash whose key is equal to deleted key. Example #1: Ruby # Ruby code for delete() method # declaring Hash value 1 min read Like