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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:27:18+00:00 2026-06-06T21:27:18+00:00

I am trying to merge two arrays of objects in ruby. The objects have

  • 0

I am trying to merge two arrays of objects in ruby. The objects have two relevant fields; id and reach_cost.

I want my resultant array to contain unique ids, where each object has the smallest reach_cost in the case of a collision.

Running;

result = a1 | a2;

Yields mixed results, it appears that elements of a1 take precedence over elements of a2.

I could of course iterate over both arrays and make the comparison on element.reach_cost manually, but this is a high performance environment, and this method gets called an awful lot. For that reason I am trying to leverage the native components of the | operator.

Is it possible to direct the | operator to prefer one object over another? Perhaps by overriding <=> or similar?

I have read the source code of the | operator in the docs but it doesn’t appear to make any comparison, merely preferring the first array parameter over the second.

  • 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-06T21:27:19+00:00Added an answer on June 6, 2026 at 9:27 pm

    I suggest storing these arrays as hashes. This provides O(1) id lookup. Here’s what the code may look like:

    h1 = {'1' => {:val => 2}, '2' => {:val => 3}}
    h2 = {'1' => {:val => 5}, '2' => {:val => 1}}
    
    def merge_hashes a, b
      a.reduce({}) do |memo, obj|
        k, v = obj
    
        # choose element with smallest :val
        memo[k] = if b[k][:val] < v[:val]
                    b[k]
                  else
                    v
                  end
    
        memo
      end
    end
    
    merge_hashes h1, h2 # => {"1"=>{:val=>2}, "2"=>{:val=>1}}
    

    This should work pretty fast.

    If you like one-liners, here’s one (kindly provided by @steenslag in the comments):

    h1.merge(h2){|key,old,new| old[:val] < new[:val] ? old : new}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

im trying to merge two arrays together. both have numeric keys and are unique.
I have two arrays which I'm trying to merge based on the date of
I'm trying to merge two arrays that have some overlapping results and some that
I have two arrays, that I'm simply trying to merge together in a function:
I'm trying to merge two arrays for the values of array1 that are set
What i'm trying to do is pretty simple.I want to merge two maps. Say
I am trying to create a new array from two current arrays. Tried array_merge,
I have been trying to code merge sort, without creating additional arrays for keeping
I am trying to merge two list in parallel. I have two sorted lists
I'm trying to merge to sorted arrays into a third sorted array , but

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.