Ruby | Hash invert() function Last Updated : 07 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report Hash#invert() is a Hash class method which gives the hash by reverting keys to values and values to key. Syntax: Hash.invert() Parameter: Hash values Return: hash by reverting keys to values and values to key Example #1 : Ruby # Ruby code for Hash.invert() 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} # invert Value puts "Hash a invert form : #{a.invert()}\n\n" puts "Hash b invert form : #{b.invert()}\n\n" puts "Hash c invert form : #{c.invert()}\n\n" Output : Hash a invert form : {100=>:a, 200=>:b} Hash b invert form : {100=>:a, 300=>:c, 200=>:b} Hash c invert form : {100=>:a} Example #2 : Ruby # Ruby code for Hash.invert() 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} # invert Value puts "Hash a invert form : #{a.invert()}\n\n" puts "Hash b invert form : #{b.invert()}\n\n" puts "Hash c invert form : #{c.invert()}\n\n" Output : Hash a invert form : {100=>"a", 200=>"b"} Hash b invert form : {100=>"a"} Hash c invert form : {100=>"a", 300=>"c", 200=>"b"} Comment More infoAdvertise with us Next Article Ruby | Range hash() function K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby Hash-class Similar Reads Ruby | Hash inspect() function Hash#inspect() is a Hash class method which gives the string representation of hash. Syntax: Hash.inspect()Parameter: Hash valuesReturn: string representation of the hash Example #1 :Â Ruby # Ruby code for Hash.inspect() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a: 1 min read Ruby | Range hash() function The hash() is an inbuilt method in Ruby returns a hash-code for the given range. The hash-value will vary for every execution. Syntax: range1.hash() Parameters: The function accepts no parameter. Return Value: It returns a hash-code for the given range. Example 1: Ruby # Ruby program for hash() # me 1 min read Ruby | Range hash() function The hash() is an inbuilt method in Ruby returns a hash-code for the given range. The hash-value will vary for every execution. Syntax: range1.hash() Parameters: The function accepts no parameter. Return Value: It returns a hash-code for the given range. Example 1: Ruby # Ruby program for hash() # me 1 min read Ruby | Hash include?() function Hash#include?() is a Hash class method which checks whether the given key is present in hash. Syntax: Hash.include?() Parameter: Hash values Return: true - if given key"" is present in hash otherwise return false Example #1 : Ruby # Ruby code for Hash.has_value?() method # declaring Hash value a = { 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 Ruby | Hash merge function Hash#merge() is a Hash class method which combines two hash arrays and their content. Syntax: Hash.merge() Parameter: Hash values Return: combine two hash arrays Example #1 : Ruby # Ruby code for Hash.merge() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c:300, 2 min read Like