We can use the values
method to return all the values of a hash in Ruby.
hash.values
There are no parameters for the values
method.
An array is returned which contains all values of Hash hash
.
Let’s look at the code below:
# create 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 the values of each# hash and print resultputs "#{h1.values}"puts "#{h2.values}"puts "#{h3.values}"puts "#{h4.values}"
values
method on the created hashes to get all their values. Then the results are printed to the console.