A particular value can be checked to see if it exists in a certain hash by using the has_value?()
method. This method returns true
if such a value exists, otherwise false
.
hash.has_value?(value)
hash
: The hash we want to use for checking.
value
: The value we want to confirm if present in the hash
.
This method returns a Boolean. true
is returned if such value exists. Otherwise, false
is returned.
# 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"}# check for some valuesputs h1.has_value?(2)puts h2.has_value?("ruby")puts h3.has_value?("foo")puts h4.has_value?("Apple")
Lines 2-5: We create several hashes.
Lines 8-11: We check if some values existed in the hashes we created and print the results to the console.