[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

newbie q: stripping duplicates

pierodancona

10/11/2008 1:34:00 PM

This is certainly well known, but not to me.

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

>> a.uniq
=> [{"aa"=>"bb"}, {"aa"=>"bb"}]

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

Thank you
Piero
8 Answers

Stefano Crocco

10/11/2008 1:38:00 PM

0

Alle Saturday 11 October 2008, pierodancona@gmail.com ha scritto:
> This is certainly well known, but not to me.
>
> >> a = [{"aa"=>"bb"},{"aa"=>"bb"}]
>
> => [{"aa"=>"bb"}, {"aa"=>"bb"}]
>
> >> a.uniq
>
> => [{"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

Craig Demyanovich

10/11/2008 1:46:00 PM

0

[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

pierodancona

10/11/2008 1:49:00 PM

0

>
> 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

Marcin Mielzynski

10/11/2008 1:59:00 PM

0

pierodancona@gmail.com pisze:
> This is certainly well known, but not to me.
>
>>> a = [{"aa"=>"bb"},{"aa"=>"bb"}]
> => [{"aa"=>"bb"}, {"aa"=>"bb"}]
>
>>> a.uniq
> => [{"aa"=>"bb"}, {"aa"=>"bb"}]
>
> Why? and, what should I use instead of .uniq
> to remove the duplicate?
>

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/pas...

lopex

pierodancona

10/11/2008 2:08:00 PM

0

On Oct 11, 3:59 pm, Marcin Mielzynski <l...@gazeta.pl> wrote:
>
> you need to monkey patch Hash:http://pastie.org/pas...
>

Monkeypatched. What a shame.

Thanks!
Piero


TPReal

10/11/2008 2:32:00 PM

0

Marcin MielżyÅ?ski wrote:
> 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/pas...

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.
--
Posted via http://www.ruby-....

Sebastian Hungerecker

10/11/2008 3:13:00 PM

0

Thomas B. wrote:
> 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
--
NP: Die Apokalyptischen Reiter - Sehnsucht
Jabber: sepp2k@jabber.org
ICQ: 205544826

Marcin Mielzynski

10/11/2008 3:17:00 PM

0

Thomas B. pisze:
> Marcin MielżyÅ?ski wrote:
>> 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/pas...
>
> 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