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 code for Hash.keys() 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} # keys Value puts "Hash a keys form : #{a.keys()}\n\n" puts "Hash b keys form : #{b.keys()}\n\n" puts "Hash c keys form : #{c.keys()}\n\n" |
chevron_right
filter_none
Output :
Hash a keys form : [:a, :b] Hash b keys form : [:a, :c, :b] Hash c keys form : [:a]
Example #2 :
# Ruby code for Hash.keys() 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} # keys Value puts "Hash a keys form : #{a.keys()}\n\n" puts "Hash b keys form : #{b.keys()}\n\n" puts "Hash c keys form : #{c.keys()}\n\n" |
chevron_right
filter_none
Output :
Hash a keys form : ["a", "b"] Hash b keys form : ["a"] Hash c keys form : ["a", "c", "b"]
Recommended Posts:
- Ruby | Hash any?() function
- Ruby | Hash each() function
- Ruby | Hash dig() function
- Ruby | Hash key?() function
- Ruby | Hash key() function
- Ruby | Hash eql? function
- Ruby | Regexp hash() function
- Ruby | Hash compare_by_identity () function
- Ruby | Hash to_s() function
- Ruby | Range hash() function
- Ruby | Hash has_key?() function
- Ruby | Hash keep_if() function
- Ruby | Hash compact() function
- Ruby | Hash compact!() function
- Ruby | Hash delete_if() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



