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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:16:31+00:00 2026-06-12T09:16:31+00:00

One tutorial has this block of code: h1 = [a => 111, b =>

  • 0

One tutorial has this block of code:

h1 = ["a" => 111, "b" => 222]
h2 = ["b" => 333, "c" => 444]
h1.merge(h2) {|key, old, new| new}
# => ["a" => 111, "b" => 333, "c" => 444]
h1.merge(h2) {|key, old, new| old}
# => ["a" => 111, "b" => 222, "c" => 444]

We have a conflict with the merge. Two duplicate keys across our two arrays. new is capturing h2‘s value for "b", and outputting it forces it to take priority in the merge. Similarly, old is capturing h1‘s value for "b", and forces it to take priority.

Why are we seemingly only grabbing the values for "b"? Shouldn’t the variables be grabbing the whole array(s)? Are the block variables context-sensitive and change function when used with .merge as opposed to, say, .times? Or is it that they change function when we’re dealing with arrays instead of simple numericals? Or is it that they see a merge conflict and are like “we got to capture the point of conflict”? I thought that maybe trying to see what was in key would help,

h1.merge(h2) {|key, old, new| key}
# => ["a" => 111, "b" => "b", "c" => 444]

but it actually just made me more confused. Why is one variable capturing the key, whereas the others are capturing the two different values?

  • 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-06-12T09:16:32+00:00Added an answer on June 12, 2026 at 9:16 am

    It’s merging: in h1.merge(h2), ‘h1’ is the “old” hash, and ‘h2’ is the “new” one. The block is called only for keys that have values in both hashes. The block gets 3 arguments: each key and the corresponding values from each hash. The logic in the block chooses which one to put in the output hash for that key. Any key that is only in one hash goes straight into the output without being processed through the block.

    You might better understand what’s going on by running

     h1.merge(h2){|key,left,right| print "k=#{key} l=#{left} r=#{right} "}
    

    From the Ruby-Doc.org description:

    Returns a new hash containing the contents of other_hash and the
    contents of hsh. If no block is specified, the value for entries with
    duplicate keys will be that of other_hash. Otherwise the value for
    each duplicate key is determined by calling the block with the key,
    its value in hsh and its value in other_hash.

    So to answer one of your specific questions, yes, the block is called conditionally, only for conflicting keys, unlike methods like .each or .delete_if, which are called for each key.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From this tutorial . see i have one pre-built static library named as stackoverflow.a
i have been looking for a tutorial for a scientific calculator one the web
I have a question with reference to this tutorial http://rox-xmlrpc.sourceforge.net/niotut/ In the read method
I've been pretty good at fixing my Xcode Obj-C errors, but this one has
I'm currently learning Win32 using this tutorial , and I have a hard time
I'm going through a Haskell tutorial and am given this piece of code to
I am working with a code base that has this single table inheritance hierarchy:
I'll preface this with I'm really new to working on Android. So I have
I've noticed in more than one SQLite tutorial that the table is recreated in
How do you do it? I saw one video tutorial on it, but the

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.