tags:

views:

278

answers:

2
@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)

+2  A: 

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
Lolindrath
Please see my edit. That was a typo
Sanjay
Yeah thats exactly what I meant. Pretty strange that adding .nil? makes it work.
Sanjay
The issue was that .add? returns nil on SUCCESS and the object on FAILURE, so you were testing the revers condition, since nil is falsey in Ruby
rampion
that incorrect, it returns nil if the object is already in the set - http://www.ruby-doc.org/core/classes/Set.html#M001630
Lolindrath
A: 

Garbled how? What kind of object is results? If results is a Set or a Hash, then you're not guaranteed that any two traversals of results will be in the same order.

Also, you could do

@search_results = results.uniq

if results is an Array to get all the unique results.

------------------------------------------------------------- Array#uniq
     array.uniq   -> an_array
------------------------------------------------------------------------
     Returns a new array by removing duplicate values in self.

        a = [ "a", "a", "b", "b", "c" ]
        a.uniq   #=> ["a", "b", "c"]
rampion
Please see my edit. That was a typo
Sanjay