newbie q: stripping duplicates

P

pierodancona

This is certainly well known, but not to me.
a = [{"aa"=>"bb"},{"aa"=>"bb"}]
=> [{"aa"=>"bb"}, {"aa"=>"bb"}]
=> [{"aa"=>"bb"}, {"aa"=>"bb"}]

Why? and, what should I use instead of .uniq
to remove the duplicate?

Thank you
Piero
 
S

Stefano Crocco

Alle Saturday 11 October 2008, (e-mail address removed) ha scritto:
This is certainly well known, but not to me.
a = [{"aa"=>"bb"},{"aa"=>"bb"}]

=> [{"aa"=>"bb"}, {"aa"=>"bb"}]

=> [{"aa"=>"bb"}, {"aa"=>"bb"}]

Why? and, what should I use instead of .uniq
to remove the duplicate?

Thank you
Piero

It works for me (with ruby-1.8.7-p72):

irb(main):005:0> a = [{"aa" => "bb"}, {"aa" => "bb"}]
=> [{"aa"=>"bb"}, {"aa"=>"bb"}]
irb(main):006:0> a.uniq
=> [{"aa"=>"bb"}]

Which version of ruby are you using?

Stefano
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

I get the same results as Piero on 1.8.6 p114, the most recent built-in
version on Mac OS X 10.5.5 (unless an update was included in the recent
security update that I haven't yet applied).
a = [{"aa"=>"bb"},{"aa"=>"bb"}] => [{"aa"=>"bb"}, {"aa"=>"bb"}]
a.uniq => [{"aa"=>"bb"}, {"aa"=>"bb"}]
exit
slapshot:~ cdemyanovich$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

Craig
 
P

pierodancona

It works for me (with ruby-1.8.7-p72):

irb(main):005:0> a = [{"aa" => "bb"}, {"aa" => "bb"}]
=> [{"aa"=>"bb"}, {"aa"=>"bb"}]
irb(main):006:0> a.uniq
=> [{"aa"=>"bb"}]

Which version of ruby are you using?

Stefano

$ ruby --version
$ ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]

I guess I am out of luck?
In case, any 1.8.6 solution?
Piero
 
T

Thomas B.

Marcin said:
1.8.7 and 1.9.x use deep hashing for hashes, to achieve that in 1.8.6
you need to monkey patch Hash: http://pastie.org/pastes/272194

So I believe this is sort of a bug in the old version? Because now I can
even get to this absurd:

irb(main):007:0> z={a[0]=>:x,a[1]=>:y}
=> {{"aa"=>"bb"}=>:x, {"aa"=>"bb"}=>:y} # absurd number 1
irb(main):008:0> z[{"aa"=>"bb"}]
=> nil # absurd number two

with the three {"aa"=>"bb"} object still reported to be ==.

TPR.
 
S

Sebastian Hungerecker

Thomas said:
with the three {"aa"=>"bb"} object still reported to be ==.

Yes, but not eql?. In 1.8.6 there were no Hash#hash and Hash#eql? methods so
it used Object#hash and Object#eql?, which considers two objects equal only
when they're actually the same object.

HTH,
Sebastian
 
M

Marcin Mielżyński

Thomas B. pisze:
Marcin said:
1.8.7 and 1.9.x use deep hashing for hashes, to achieve that in 1.8.6
you need to monkey patch Hash: http://pastie.org/pastes/272194

So I believe this is sort of a bug in the old version? Because now I can
even get to this absurd:

irb(main):007:0> z={a[0]=>:x,a[1]=>:y}
=> {{"aa"=>"bb"}=>:x, {"aa"=>"bb"}=>:y} # absurd number 1
irb(main):008:0> z[{"aa"=>"bb"}]
=> nil # absurd number two

with the three {"aa"=>"bb"} object still reported to be ==.

It's not an absurd, just a consequence Hash doesn't have it's own hash
(just default Object#hash). I agree it's a bit surprising but deep
hashing is slower by a fair amount.

Comparison function will not even be called here since it fails earlier,
at hash bucket lookup:

a = {"aa"=>"bb"}
b = {"aa"=>"bb"}
a.hash != b.hash

Btw, Hash doesn't use "==" for object comparison, it uses "eql?"

lopex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top