Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on merge function in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 530
    Comment on it

    Welcome to Findnerd. Today we are going to discuss merge function in Ruby. If we talk about the merge function then it is used for hashes only. We can merge two hashes in one hash. Please have a look.

     

    teacher1 = { "id" => 232 ,"name" => "Deepak"  }
    teacher2 = { "id" => 232 ,"name" => "Verma", "salary" => 50000 }
    teacher1.merge(teacher2);
    # { "id" => 232, "name" => "Verma" , "salary" => 50000 }
    teacher2.merge(teacher1);
    # { "id" => 232, "name" => "Deepak" , "salary" => 50000 }

     

     

    In above code we have two different hashes of a teacher details. We can merge these hashes in two different ways. We can merge first hash into second hash and vice versa.

     

    teacher1.merge(teacher2) { |key,old,new| new }
    # { "id" => 232, "name" => "Verma" , "salary" => 50000 }
    
    teacher1.merge(teacher2) { |key,old,new| old }
    # { "id" => 232, "name" => "Deepak" , "salary" => 50000 }

     

     

    In above example you can see, we are merging hashes as new and old values of name key.

     

    teacher1.merge(teacher2) { |key,old,new| old + "Verma" }
    # { "id" => 232, "name" => "Deepak Verma" , "salary"=> 50000 }
    
    teacher1.merge(teacher2) { |key,old,new|  "Deepak" + old  }
    # { "id" => 232, "name" => "Deepak Verma" , "salary"=> 50000 }

     

     

    In above code we are trying to combine first and last name from both hashes. At the time of merging both hashes we can prefer new or old value for the same key in both hashes.

     

    h1 = { "a" => 222, "b" => 333 }
    
    h2 = { "a" => 444, "c" => 555 }
    
    h1.merge(h2) do |key,old,new|
    
    if old < new
    new
    else
    old
    end
    # { "a" => 444, "b" => 333, "c" => 555 }

     

     

    In above code we are comparing old value with new value and returning the new value if it is bigger than the old one. You can also write above code in short lines.

     

    h1.merge(h2) { |k,o,n| o<n ? n : o }
    # { "a" => 444, "b" => 333, "c" => 555 }

     

     

    All above examples are returning new hash instead of changing the existing hashes. You can make the changes in existing hashes if required. Please have a look.

     

    h1.merge!(h2) 
    # { "a" => 444, "b" => 333, "c" => 555 }
    h1 {"a" => 444, "b" => 333}
    h2 { "a" => 444, "c" => 555 }

     

     

    In above code merge function is also updated the value of a key name in first hash.

     

    Thank you for being with us!

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: