Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 445685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:23:44+00:00 2026-05-12T21:23:44+00:00

Say I have the following hashes: hash_x = { :a => 1, :b =>

  • 0

Say I have the following hashes:

hash_x = {
  :a => 1,
  :b => 2
}
hash_y = {
  :b => 2,
  :c => 3
}

I need a chunk of logic that compares the two for equality only taking into consideration intersecting keys.

In this example the ‘b’ key is the only commonality between the two hashes and it’s value is set to ‘2’ in both so by that logic these two hashes would be considered equal.

Likewise these two hashes would not be equal due to the inequality of the ‘d’ key (the ‘a’ and ‘c’ key values are ignored since they are unique to their respective hashes):

hash_p = {
  :a => 1,
  :b => 2,
  :d => 3,
}
hash_q = {
  :b => 2,
  :c => 3,
  :d => 4
}

Is there a clever one-liner in Ruby that can calculate the intersecting keys of the two hashes then compare their values for equality based on those keys?

Bonus points if you provide tests.

More bonus points if you monkey-patch it into the Hash class.

  • 1 1 Answer
  • 4 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:23:45+00:00Added an answer on May 12, 2026 at 9:23 pm
    def compare_intersecting_keys(a, b)
      (a.keys & b.keys).all? {|k| a[k] == b[k]}
    end
    

    Use like this:

    compare_intersecting_keys(hash_x, hash_y)  # => true
    compare_intersecting_keys(hash_p, hash_q)  # => false
    

    If you want it monkey-patched:

    class Hash
      def compare_intersection(other)
        (self.keys & other.keys).all? {|k| self[k] == other[k]}
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have the following XML <?xml version=1.0 encoding=utf-8?> <Person> <FirstName>Bjorn</FirstName> <LastName>Ellis-Gowland</LastName> </Person> That
I have a very specific deserialization need, see example below: say I have following
I have following scenario: Lets say we have two different webparts operating on the
Let's say I have following code cursor = connection.cursor() cursor.execute(query) after that point I
Say that have the following CTE that returns the level of some tree data
Let's say I have following classes. (only most important things included) public class Client
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
Say I have the following: namespace SomeProject { public interface ISomething { string SomeMethod();
Say I have the following basic if-statement: if (A ~= 0) % do something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.