Getting the keys of a hash at once in Ruby is possible using the keys
property. It returns a new array that contains all the keys of a hash.
hash.keys
The key
property does not take any arguments.
The value returned is an array containing all keys of the hash, hash
.
# create some hashesh1 = {one: 1, two: 2, three: 3}h2 = {name: "okwudili", stack: "ruby"}h3 = {"foo": 0, "bar": 1}h4 = {M:"Mango", A: "Apple", B: "Banana"}# get all keys of the hashes# and print resultsputs "#{h1.keys}"puts "#{h2.keys}"puts "#{h3.keys}"puts "#{h4.keys}"
keys
property to get the keys of the hashes. We use the puts
function to print the result.