0

I have two hashes like below,

h1 = {"a" => 1, "b" => 2, "c" => 3}
h2 = {"a" => 2, "b" => 2, "d" => 3}

I want to iterate over hash1 and hash2 and find matching keys and their values and print it on console. Example here it should return output "b" => 2 .its not working with below code,

h1.each do |key1, value1|

  h2.each do |key2, value2|
    if ((h2.include? key1) && (h2.include? value1))
      puts "matching h2 key #{h2[key2]}and h1 key #{h1[key1]}"
    else
      puts " don not match h2 key #{h2[key2]}and h1 key #{h1[key1]}"
    end
  end
end

I am from basically C++ and Java background and its very easy to do using for loops and iterators, but using Ruby, it is very difficult.

2
  • 1
    Your question is unclear. What does "it's not working" mean? How is it not working? Do you get an error? If yes, what is the precise error message you are getting, what is the backtrace, and what line does the error occur on? Dou you get a warning? If yes, what is the precise warning message and what line does the warning occur on? Does the observed behavior differ from the expected behavior? If yes, what is the observed behavior and what is the expected behavior? Please provide a specification of what exactly it is you want to do, including any and all exceptions, special cases, … Commented Feb 19, 2017 at 13:07
  • 1
    … corner cases, and edge cases. Also, please provide sample inputs and outputs as test cases, including examples of exceptions, corner cases, special cases, and edge cases. In the title, you are asking about "matching values". The result I would expect for matching values is [{"b" => 2}, {"c" => 3}, {"d" => 3}]. In the question body, you are asking about "matching keys". The result I would expect for matching keys is [{"a" => 1}, {"a" => 2}, {"b" => 2}]. However, neither of those corresponds to your desired result of {"b" => 2}. Commented Feb 19, 2017 at 13:15

4 Answers 4

2
h1.merge(h2) { |k,o,n| puts "#{k}=>#{o}" if o == n }
"b" => 2

This uses the form of Hash#merge that employs a bock to determine the values of keys that are present in both hashes being merged. See the doc for details.

Sign up to request clarification or add additional context in comments.

Comments

1

The second loop is not required.

h1.each do |key1, value1|
  if (h2.include? key1) and (h2[key1] == value1)
    puts "Match #{key1} with value #{value1}"
  else
    puts "#{key1} does not match"
  end
end

Comments

1

You might write something like

h1.each do |k,v|
  if h2[k] == v
    puts "matched key = #{k} and value = #{v}"
  else
    puts "NOT matched key = #{k} and value = #{v}"
  end
end

Output

NOT matched key = a and value = 1
matched key = b and value = 2
NOT matched key = c and value = 3

Comments

1

If the result is the main objective, select can work too:

h1.select{|k,v| h2[k] == v }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.