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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T03:15:15+00:00 2026-05-11T03:15:15+00:00

@search_results = Array.new duplicates = Set.new results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url]) } This

  • 0
@search_results = Array.new duplicates = Set.new results.each { |result|    @search_results.push(result) unless duplicates.add?(result[:url]) } 

This piece of code is garbling the order of elements in the array @search_results. Why would inserting the same element in a set and an array change the insertion order for Array? Seems like some issue with element references. Can someone explain?

Edit 1: I am using an Array. Sorry for the earlier typo. I double checked by code and it uses Array too (there is no push method for Hash anyways)

  • 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. 2026-05-11T03:15:15+00:00Added an answer on May 11, 2026 at 3:15 am

    The order of elements in a Hash is not guaranteed. You’ll have to sort the keys if you want a guaranteed order.

    This is supposedly fixed in Ruby 1.9 I believe.

    Edit: I’m assuming your results in an Array, if its a Hash then order isn’t guaranteed and you’ll have to sort the keys, here’s what my test looks like:

    #!/usr/bin/ruby -W  require 'pp' require 'set'  results = Array.new  results << {:url => 'http://lifehacker.com'} results << {:url => 'http://stackoverflow.com'} results << {:url => 'http://43folders.com'} results << {:url => 'http://lolindrath.com'} results << {:url => 'http://stackoverflow.com'} results << {:url => 'http://lifehacker.com'}    @search_results = Array.new duplicates = Set.new  results.each { |result| @search_results.push(result) unless duplicates.add?(result[:url])}  puts '## @search_results' pp @search_results 

    If I run that, here’s the result:

    ## @search_results [{:url=>'http://stackoverflow.com'}, {:url=>'http://lifehacker.com'}] 

    I found that odd, so just to be sure, I put a .nil? add the end of .add? and here was my result:

    ## @search_results [{:url=>'http://lifehacker.com'},  {:url=>'http://stackoverflow.com'},  {:url=>'http://43folders.com'},  {:url=>'http://lolindrath.com'}] 

    Now that was what I was expecting: is this what you mean by ‘garbled’?

    Edit 2: Upon further investigation, I think this is because of Ruby’s super strict rules when converting non-Boolean data to Booleans (see Ruby Gotchas on Wikipedia and Stack Overflow, of course) so that basically anything that only false is really false and everything else is true. so the .nil? is converting it explicitly to true/false.

    irb(main):007:0> puts 'zero is true' if 0 zero is true => nil irb(main):008:0> puts 'zero is false' unless 0 => nil 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I call eBay and request it to return search results array? This
Using this code: var pendingRequest = new Ajax.Request(myUrl, { method: 'post', postBody: soapMsg, contentType:
I have this piece of code: return json_decode($body); This return is from a function
I have been trying to use array_unique to remove duplicates from the search results
I am trying to dynamically filter search results. Every result has tags associated with
The array looks something like this: $array = array( array( 'Item' => array(47, 48,
I am very new to Javascript and Jquery, so my apologies for this beginner's
I'm new in PHP, it's learning process. Now I'm getting tired to fix this
I have got all the search results for nearby places in an array list,
I have a huge array coming back as search results and I want to

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.