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 3855808
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:50:00+00:00 2026-05-19T17:50:00+00:00

I am trying to compare two Ruby Hashes using the following code: #!/usr/bin/env ruby

  • 0

I am trying to compare two Ruby Hashes using the following code:

#!/usr/bin/env ruby

require "yaml"
require "active_support"

file1 = YAML::load(File.open('./en_20110207.yml'))
file2 = YAML::load(File.open('./locales/en.yml'))

arr = []

file1.select { |k,v|
  file2.select { |k2, v2|
    arr << "#{v2}" if "#{v}" != "#{v2}"
  }
}

puts arr

The output to the screen is the full file from file2. I know for a fact that the files are different, but the script doesn’t seem to pick it up.

  • 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-19T17:50:00+00:00Added an answer on May 19, 2026 at 5:50 pm

    You can compare hashes directly for equality:

    hash1 = {'a' => 1, 'b' => 2}
    hash2 = {'a' => 1, 'b' => 2}
    hash3 = {'a' => 1, 'b' => 2, 'c' => 3}
    
    hash1 == hash2 # => true
    hash1 == hash3 # => false
    
    hash1.to_a == hash2.to_a # => true
    hash1.to_a == hash3.to_a # => false
    


    You can convert the hashes to arrays, then get their difference:

    hash3.to_a - hash1.to_a # => [["c", 3]]
    
    if (hash3.size > hash1.size)
      difference = hash3.to_a - hash1.to_a
    else
      difference = hash1.to_a - hash3.to_a
    end
    Hash[*difference.flatten] # => {"c"=>3}
    

    Simplifying further:

    Assigning difference via a ternary structure:

      difference = (hash3.size > hash1.size) \
                    ? hash3.to_a - hash1.to_a \
                    : hash1.to_a - hash3.to_a
    => [["c", 3]]
      Hash[*difference.flatten] 
    => {"c"=>3}
    

    Doing it all in one operation and getting rid of the difference variable:

      Hash[*(
      (hash3.size > hash1.size)    \
          ? hash3.to_a - hash1.to_a \
          : hash1.to_a - hash3.to_a
      ).flatten] 
    => {"c"=>3}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to compare two Xml files using C# code. I want to ignore
Trying to create a QtRuby application, I get the following error: /usr/lib64/ruby/site_ruby/1.8/Qt/qtruby4.rb:2144: [BUG] Segmentation
I'm trying to compare two DataRow s in a loop. However, the following if
I am trying to compare two large Visual Studio 2010 solutions using Beyond Compare.
I have the following problem. I am trying to compare two datetime values in
I am trying to compare two decimal values in Java script. I have two
I trying to compare two dates (DateTime) in nHibernate linq: query = query.Where(l =>
I am trying to compare two .NET arrays. Here is an obvious implementation for
I am trying to compare two long bytearrays in VB.NET and have run into
I'm trying to compare two different object in JSF. A String and an Integer,

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.