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

  • SEARCH
  • Home
  • 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 7624297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:55:33+00:00 2026-05-31T04:55:33+00:00

The complement is the mathematical term for what I’m looking for, but for context

  • 0

The complement is the mathematical term for what I’m looking for, but for context and possibly more targeted solution: I have hash A, which can have nested hashes (i.e. they’re N-dimensional), and I apply to it a process (over which I have no control) which returns hash B, which is hash A with some elements removed. From there on, I am trying to find the elements in A which have been removed in B.

For example: (note that I use symbols for simplicity. Keys will always be symbols, but values won’t.)

a = {:a => :b,
     :c => {:d => :e, :f => :g},
     :h => :i,
     :j => {:k => :l, :m => :n},
     :o => {:p => :q, :r => :s},
     :t => :u}

b = {:h => :i,
     :j => {:k => :l, :m => :n},
     :o => {:r => :s},
     :t => :u}

complement(a,b)
#=> {:a => :b,
#    :c => {:d => :e, :f => :g},
#    :o => {:p => :q}}

What is the best (ruby-esque) way of doing this?

  • 1 1 Answer
  • 0 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-31T04:55:35+00:00Added an answer on May 31, 2026 at 4:55 am

    Came up with this

    a = {a: "thing", b: [1,2,3], c:2}
    b = {a: "thing", b: [1,2,3]}
    c= {}
    a.each do |k, v|
      c[k] = v unless b[k]
    end
    p c
    

    EDIT: Now checking nested hashes. But yes, there should be some better ruby way to do this.

    def check_deleted(a, b)
        c = Hash.new
        a.each do |k, v|
            if ! b.has_key? k
                c[k] = v
            elsif b[k].is_a? Hash
                c[k] = check_deleted(v, b[k])
            end
        end
        c
    end
    a = {a: "thing", b: [1,2,3], c:2, d: {e: 1, r:2}}
    b = {a: "thing", b: [1,2,3], d: {r:2}}
    
    p check_deleted(a,b) #=> {:c=>2, :d=>{:e=>1}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone shed some light on how these compare/complement each other in the context
I have signed numbers (2s complement) stored in 32-bit integers, and I want to
I have a number (hex) and I want the one's complement of it. For
We have 2's complement for integers that allows us to perform operations without worrying
I have assigned the complement value in an unsigned variable. Then why this C
I basically want to do a complement set operation in SQLite but not sure
Do you have any recommendations on great video courses as a complement to books
If I have a 32 bit two's complement number and I want to know
We'd find it very useful to have an AddListener to complement RemovalListener in Google
In Java BigDecimal class contains values as A*pow(10,B) where A is 2's complement which

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.