Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using new Ruby pattern matching to check if a hash has certain keys

I want to use the new Ruby 3 feature in this very simple case. I know it must be possible but I have not figured it out from the documentation.

Given a hash, I want to check that it has certain keys. I don’t mind if it has others in addition. And I want to do this with pattern matching (or know that it is impossible.) I also don’t want to use a case statement which seems overkill.

{name: "John", salary: 12000, email: "john@email.com" } 
  1. Raise an error if the hash does not have name, and email as strings and salary as a number.

    MEDevel.com: Open-source for Healthcare and Education

    Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

    Visit Medevel

  2. Use the contruct in an if or other conditional?

  3. And what if the hash has strings as keys (which is what I get from JSON.parse) ?

    {"name" => "John", "salary" => 12000, "email" => "john@email.com" }

>Solution :

You’re looking for the => operator:

h = {name: "John", salary: 12000, email: "john@email.com" }
h => {name: String, salary: Numeric, email: String} # => nil

With an additional pair (test: 0):

h[:test] = 0
h => {name: String, salary: Numeric, email: String} # => nil

Without the :name key:

h.delete :name
h => {name: String, salary: Numeric, email: String} # key not found: :name (NoMatchingPatternKeyError)

With the :name key but the class of its value shouldn’t match:

h[:name] = 1
h => {name: String, salary: Numeric, email: String} # String === 1 does not return true (NoMatchingPatternKeyError)

A strict match:

h[:name] = "John"
h => {name: String, salary: Numeric, email: String} # => rest of {:test=>0} is not empty

The in operator returns a boolean value instead of raising an exception:

h = {name: "John", salary: 12000, email: "john@email.com" }
h in {name: String, salary: Numeric, email: String} # => true
h[:name] = 1
h in {name: String, salary: Numeric, email: String} # => false
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading