The Ruby has_key? technique merely checks to see if a hash has a details secret or otherwise. If it does, real will certainly be returned. If the secret is not located, incorrect will certainly be returned.

 some_hash = {"first_name" => > "David", "last_name" => > "Smith"}
places some_hash. has_key? "first_name".
#Output.
real.

The has_key? technique is rather simple yet can be truly helpful when looking for details kept in a hash.

Let’s take a look at some even more instances of the has_key? technique.

 some_hash = {"id" => > "1", "first_name" => > "David", "last_name" => > "Smith", "age" => > "45"}
places some_hash. has_key? "first_name".
places some_hash. has_key? "David".
places some_hash. has_key? "last_name".
places some_hash. has_key? "age".
places some_hash. has_key? "".
#Output.
real.
incorrect.
real.
real.
incorrect.

Let’s have a look at the has_value? technique in Ruby to obtain the various other worths in our hash.

Inspect If Worth Exists in Hash Utilizing the Ruby has_value? Method

We checked out the has_key? technique over to see if a particular secret was had in our hash. Currently we can utilize the has_value? technique to see if particular worths are had in our hash.

 some_hash = {"first_name" => > "David", "last_name" => > "Smith"}
places some_hash. has_value? "David".
#Output.
real.

Let’s run the exact same instances over, yet this moment look for particular worths that could be had in the hash.

 some_hash = {"id" => > "1", "first_name" => > "David", "last_name" => > "Smith", "age" => > "45"}
places some_hash. has_value? "first_name".
places some_hash. has_value? "David".
places some_hash. has_value? "last_name".
places some_hash. has_value? "45".
places some_hash. has_value? 45.
#Output.
incorrect.
real.
incorrect.
real.
incorrect.

Hopefully this post has served for you to discover exactly how to the Ruby has_key? technique functions.

Concerning The Programs Expert

The Programs Specialist is a collection of a developer’s searchings for worldwide of software program advancement, site development, and also automation of procedures.

Programs enables us to develop fantastic applications that make our job much more reliable, repeatable and also exact.

At the end of the day, we wish to have the ability to simply press a switch and also allow the code do it’s magic.

You can find out more regarding us on our about page.



Source link .