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