# Array::uniq { block }?

**URL:** <https://rubytalk.org/t/array-uniq-block/16048>\
**Category:** ruby-talk\
**Created:** [26 January 2005 15:33 UTC](https://rubytalk.org/t/array-uniq-block/16048 "2005-01-26T15:33:54Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Belorion](https://avatars.discourse-cdn.com/v4/letter/b/ea666f/32.png) [@Belorion](https://rubytalk.org/u/Belorion)\
**Post date:** [26 January 2005 15:33 UTC](https://rubytalk.org/t/array-uniq-block/16048/1 "2005-01-26T15:33:54Z")

</div>

I have an array of arrays. I want to be able to do a uniq operation  
on my array, and have it accept a block so I can specify what to  
examine for uniqueness. For example, in IRB

a = [[1,2], [3,4], [1,3] ]  
a.sort { |x,y| x[0] \<=\> y[0] }

returns =\> [[1,2], [1,3], [3,4] ]

However, it does not appear uniq accepts blocks:

a = [[1,2], [3,4], [1,3] ]  
a.uniq { |x,y| x[0] == y[0] }

returns =\> [[1,2], [3,4], [1,3] ]  
and a.uniq! with the above block returns nil.

Am I stuck writing my own deep\_uniq? Granted, it would be hard, but  
certainly not as clean as the above.

---

<div class="post-metadata">

**Author:** ![Daniel\_Berger3](https://avatars.discourse-cdn.com/v4/letter/d/c68b51/32.png) [@Daniel\_Berger3](https://rubytalk.org/u/Daniel_Berger3)\
**Post date:** [26 January 2005 16:15 UTC](https://rubytalk.org/t/array-uniq-block/16048/2 "2005-01-26T16:15:47Z")

</div>

Belorion wrote:

> I have an array of arrays. I want to be able to do a uniq operation  
> on my array, and have it accept a block so I can specify what to  
> examine for uniqueness. For example, in IRB
> 
> a = [[1,2], [3,4], [1,3] ]  
> a.sort { |x,y| x[0] \<=\> y[0] }
> 
> returns =\> [[1,2], [1,3], [3,4] ]
> 
> However, it does not appear uniq accepts blocks:
> 
> a = [[1,2], [3,4], [1,3] ]  
> a.uniq { |x,y| x[0] == y[0] }
> 
> returns =\> [[1,2], [3,4], [1,3] ]  
> and a.uniq! with the above block returns nil.
> 
> Am I stuck writing my own deep\_uniq? Granted, it would be hard, but  
> certainly not as clean as the above.

Not that this is a bad idea, but why do I get this feeling that we're  
eventually going to find an excuse to allow blocks to virtually every  
method in the core library?

Where does it all end?!

Dan

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [26 January 2005 18:00 UTC](https://rubytalk.org/t/array-uniq-block/16048/3 "2005-01-26T18:00:57Z")

</div>

"Belorion" \<belorion@gmail.com\> schrieb im Newsbeitrag news:a48d774d05012607334cd93e1e@mail.gmail.com...

> I have an array of arrays. I want to be able to do a uniq operation  
> on my array, and have it accept a block so I can specify what to  
> examine for uniqueness. For example, in IRB
> 
> a = [[1,2], [3,4], [1,3] ]  
> a.sort { |x,y| x[0] \<=\> y[0] }
> 
> returns =\> [[1,2], [1,3], [3,4] ]
> 
> However, it does not appear uniq accepts blocks:
> 
> a = [[1,2], [3,4], [1,3] ]  
> a.uniq { |x,y| x[0] == y[0] }
> 
> returns =\> [[1,2], [3,4], [1,3] ]  
> and a.uniq! with the above block returns nil.
> 
> Am I stuck writing my own deep\_uniq? Granted, it would be hard, but  
> certainly not as clean as the above.

The block is accepted but apparently not used:

> > %w{a b c d a d}.uniq {|\*x| p x}

=\> ["a", "b", "c", "d"]

Could be a bug in the std lib. (Btw, I'm on 1.8.1 here)

How about using a Hash in the meantime?

> > h = a.inject({}){|h,(k,v)| h[k] ||= [k,v];h}

=\> {1=\>[1, 2], 3=\>[3, 4]}

> > h.values

=\> [[1, 2], [3, 4]]

Or

> > h = a.inject({}){|h,pair| h[pair[0]] ||= pair;h}

=\> {1=\>[1, 2], 3=\>[3, 4]}

> > h.values

=\> [[1, 2], [3, 4]]

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [26 January 2005 18:05 UTC](https://rubytalk.org/t/array-uniq-block/16048/4 "2005-01-26T18:05:47Z")

</div>

"Robert Klemme" \<bob.news@gmx.net\> schrieb im Newsbeitrag news:35q3t9F4jlog7U1@individual.net...

> "Belorion" \<belorion@gmail.com\> schrieb im Newsbeitrag news:a48d774d05012607334cd93e1e@mail.gmail.com...
> 
> > I have an array of arrays. I want to be able to do a uniq operation  
> > on my array, and have it accept a block so I can specify what to  
> > examine for uniqueness. For example, in IRB
> > 
> > a = [[1,2], [3,4], [1,3] ]  
> > a.sort { |x,y| x[0] \<=\> y[0] }
> > 
> > returns =\> [[1,2], [1,3], [3,4] ]
> > 
> > However, it does not appear uniq accepts blocks:
> > 
> > a = [[1,2], [3,4], [1,3] ]  
> > a.uniq { |x,y| x[0] == y[0] }
> > 
> > returns =\> [[1,2], [3,4], [1,3] ]  
> > and a.uniq! with the above block returns nil.
> > 
> > Am I stuck writing my own deep\_uniq? Granted, it would be hard, but  
> > certainly not as clean as the above.
> 
> The block is accepted but apparently not used:
> 
> > > %w{a b c d a d}.uniq {|\*x| p x}
> 
> =\> ["a", "b", "c", "d"]
> 
> Could be a bug in the std lib. (Btw, I'm on 1.8.1 here)

Delete that: you can provide a block for \*every\* method - it's simply ignored. So no bug, but unique obviously is not built to use it. Stupid me...

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > How about using a Hash in the meantime?
> > 
> > > > h = a.inject({}){|h,(k,v)| h[k] ||= [k,v];h}
> > 
> > =\> {1=\>[1, 2], 3=\>[3, 4]}
> > 
> > > > h.values
> > 
> > =\> [[1, 2], [3, 4]]
> > 
> > Or
> > 
> > > > h = a.inject({}){|h,pair| h[pair[0]] ||= pair;h}
> > 
> > =\> {1=\>[1, 2], 3=\>[3, 4]}
> > 
> > > > h.values
> > 
> > =\> [[1, 2], [3, 4]]
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [26 January 2005 18:55 UTC](https://rubytalk.org/t/array-uniq-block/16048/5 "2005-01-26T18:55:47Z")

</div>

I agree, #uniq is certainly not the only candidate. I think it would be very  
useful to compile a list of all such methods that could use a block to  
override some default internal method (#== in the case of uniq), and  
consider these for 2.0.

"Daniel Berger" \<djberg96@hotmail.com\> wrote in message  
news:1106756045.223279.94600@f14g2000cwb.googlegroups.com...

> **···**
>
> > Belorion wrote:  
> > \> I have an array of arrays. I want to be able to do a uniq operation  
> > \> on my array, and have it accept a block so I can specify what to  
> > \> examine for uniqueness. For example, in IRB  
> > \>  
> > \> a = [[1,2], [3,4], [1,3] ]  
> > \> a.sort { |x,y| x[0] \<=\> y[0] }  
> > \>  
> > \> returns =\> [[1,2], [1,3], [3,4] ]  
> > \>  
> > \> However, it does not appear uniq accepts blocks:  
> > \>  
> > \> a = [[1,2], [3,4], [1,3] ]  
> > \> a.uniq { |x,y| x[0] == y[0] }  
> > \>  
> > \> returns =\> [[1,2], [3,4], [1,3] ]  
> > \> and a.uniq! with the above block returns nil.  
> > \>  
> > \> Am I stuck writing my own deep\_uniq? Granted, it would be hard, but  
> > \> certainly not as clean as the above.
> > 
> > Not that this is a bad idea, but why do I get this feeling that we're  
> > eventually going to find an excuse to allow blocks to virtually every  
> > method in the core library?
> > 
> > Where does it all end?!
> > 
> > Dan

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [27 January 2005 10:33 UTC](https://rubytalk.org/t/array-uniq-block/16048/6 "2005-01-27T10:33:38Z")

</div>

> I agree, #uniq is certainly not the only candidate. I think it would be very  
> useful to compile a list of all such methods that could use a block to  
> override some default internal method (#== in the case of uniq), and  
> consider these for 2.0.

#uniq don't use #==, it use #hash and #eql?

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 January 2005 11:07 UTC](https://rubytalk.org/t/array-uniq-block/16048/7 "2005-01-27T11:07:01Z")

</div>

"ts" \<decoux@moulon.inra.fr\> schrieb im Newsbeitrag  
news:200501271033.j0RAXMu05608@moulon.inra.fr...

> \> I agree, #uniq is certainly not the only candidate. I think it would

be very

> \> useful to compile a list of all such methods that could use a block

to

> \> override some default internal method (#== in the case of uniq), and  
> \> consider these for 2.0.
> 
> #uniq don't use #==, it use #hash and #eql?

Hm...

class Dummy  
&nbsp;&nbsp;def eql?(x) puts "eql?"; super end  
&nbsp;&nbsp;def equal?(x) puts "equal?"; super end  
&nbsp;&nbsp;def ==(x) puts "=="; super end  
&nbsp;&nbsp;def hash() puts "hash"; super end  
&nbsp;&nbsp;def id() puts "id"; super end  
end

> > h={Dummy.new =\> 1, Dummy.new =\>2}

hash  
hash  
=\> {#\<Dummy:0x10187428\>=\>2, #\<Dummy:0x10187440\>=\>1}

> > h.keys.concat(h.keys).uniq

hash  
hash  
hash  
hash  
hash  
hash  
hash  
hash  
=\> [#\<Dummy:0x10187428\>, #\<Dummy:0x10187440\>]

I don't see eql? called here - maybe it's an internal optimization that  
short circuits identity. Or did I miss something?

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [27 January 2005 11:11 UTC](https://rubytalk.org/t/array-uniq-block/16048/8 "2005-01-27T11:11:02Z")

</div>

Which raises the problem that it works globally, unlike the unix uniq  
which only uniqs consecutive elements. Passing in a block would lead to  
a performance hit since you'd need to compare every pair of elements  
without the benefit of a hash function. A uniq\_by would probably cover  
most of the use cases anyway.

martin

> **···**
>
> ts \<decoux@moulon.inra.fr\> wrote:
> 
> > \> I agree, #uniq is certainly not the only candidate. I think it would be very  
> > \> useful to compile a list of all such methods that could use a block to  
> > \> override some default internal method (#== in the case of uniq), and  
> > \> consider these for 2.0.
> > 
> > #uniq don't use #==, it use #hash and #eql?

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [27 January 2005 11:11 UTC](https://rubytalk.org/t/array-uniq-block/16048/9 "2005-01-27T11:11:35Z")

</div>

> I don't see eql? called here - maybe it's an internal optimization that  
> short circuits identity. Or did I miss something?

If 2 object have different hash value, it don't need to call eql?

Now if they have the same hash value :  
&nbsp;&nbsp;\* if eql? return true, this is the "same" object (i.e. it will use the  
&nbsp;&nbsp;&nbsp;&nbsp;same key)  
&nbsp;&nbsp;\* otherwise there is collision

Object#hash return the id of object

Guy Decoux

---

<div class="post-metadata">

**Author:** ![Its\_Me](https://avatars.discourse-cdn.com/v4/letter/i/958977/32.png) [@Its\_Me](https://rubytalk.org/u/Its_Me)\
**Post date:** [28 January 2005 02:25 UTC](https://rubytalk.org/t/array-uniq-block/16048/10 "2005-01-28T02:25:55Z")

</div>

"Martin DeMello" \<martindemello@yahoo.com\> wrote in message  
news:rZ3Kd.187524$6l.10402@pd7tw2no...

> \>  
> \> \> I agree, #uniq is certainly not the only candidate. I think it would

be very

> \> \> useful to compile a list of all such methods that could use a block

to

> \> \> override some default internal method (#== in the case of uniq), and  
> \> \> consider these for 2.0.  
> \>  
> \> #uniq don't use #==, it use #hash and #eql?
> 
> Which raises the problem that it works globally, unlike the unix uniq  
> which only uniqs consecutive elements. Passing in a block would lead to  
> a performance hit since you'd need to compare every pair of elements  
> without the benefit of a hash function.

Good points. Probably applies to many other cases that I was thinking 'Now,  
why is there no block here?'

> A uniq\_by would probably cover  
> most of the use cases anyway.

Why a different method? Is that the way it is done elsewhere? Why not  
def uniq  
&nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... less\_efficient  
&nbsp;&nbsp;&nbsp;&nbsp;else default\_way  
end

> **···**
>
> > ts \<decoux@moulon.inra.fr\> wrote:

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 January 2005 12:25 UTC](https://rubytalk.org/t/array-uniq-block/16048/11 "2005-01-27T12:25:59Z")

</div>

"ts" \<decoux@moulon.inra.fr\> schrieb im Newsbeitrag  
news:200501271111.j0RBBJu07211@moulon.inra.fr...

> \> I don't see eql? called here - maybe it's an internal optimization

that

> \> short circuits identity. Or did I miss something?
> 
> If 2 object have different hash value, it don't need to call eql?

Ah, yes of course. That's it! Thanks for reminding me.

> Now if they have the same hash value :  
> &nbsp;&nbsp;\* if eql? return true, this is the "same" object (i.e. it will use the  
> &nbsp;&nbsp;&nbsp;&nbsp;same key)  
> &nbsp;&nbsp;\* otherwise there is collision

Of all what you said I don't understand your last point: Why is there a  
collision?

> > class Foo; def hash() 0 end end

=\> nil

> > h = {Foo.new =\> 1, Foo.new =\> 2}

=\> {#\<Foo:0x101a9808\>=\>2, #\<Foo:0x101a9898\>=\>1}

> > h.keys.uniq

=\> [#\<Foo:0x101a9808\>, #\<Foo:0x101a9898\>]

And indeed a redefined Dummy clarifies this:

class Dummy  
&nbsp;&nbsp;def eql?(x) puts "eql?"; super end  
&nbsp;&nbsp;def equal?(x) puts "equal?"; super end  
&nbsp;&nbsp;def ==(x) puts "=="; super end  
&nbsp;&nbsp;def hash() puts "hash"; 0 end  
&nbsp;&nbsp;def id() puts "id"; super end  
end

> > h={Dummy.new =\> 1, Dummy.new =\> 2}

hash  
hash  
eql?  
=\> {#\<Dummy:0x1018da28\>=\>2, #\<Dummy:0x1018da88\>=\>1}

> > h.keys.uniq

hash  
hash  
eql?  
=\> [#\<Dummy:0x1018da28\>, #\<Dummy:0x1018da88\>]

Again a reminder that it's always a good idea to override #hash, #== and  
#eql? in one go for classes that should be used as hash keys or in sets.

> Object#hash return the id of object

Yeah, but that might be overridden in an arbitrary way. Normally one  
can't rely on that #hash tells anything about the identity of an instance  
other than probably that instances with different hash are not identical  
and not equivalent.

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [27 January 2005 13:46 UTC](https://rubytalk.org/t/array-uniq-block/16048/12 "2005-01-27T13:46:04Z")

</div>

Hi,

> **···**
>
> Am Donnerstag, 27. Jan 2005, 20:11:35 +0900 schrieb ts:
> 
> > \> I don't see eql? called here - maybe it's an internal optimization that  
> > \> short circuits identity. Or did I miss something?
> > 
> > If 2 object have different hash value, it don't need to call eql?
> > 
> > Now if they have the same hash value :  
> > &nbsp;&nbsp;\* if eql? return true, this is the "same" object (i.e. it will use the  
> > &nbsp;&nbsp;&nbsp;&nbsp;same key)  
> > &nbsp;&nbsp;\* otherwise there is collision
> 
> What a look at the interpreter source code will validate.  
> The elements addes as hash keys.
> 
> Strings are treated special, by the way.
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [28 January 2005 08:40 UTC](https://rubytalk.org/t/array-uniq-block/16048/13 "2005-01-28T08:40:54Z")

</div>

By analogy with sort and sort\_by

a.uniq\_by {|i| i[1]} would work like uniq, but hashing i[i] rather than  
i. a.uniq {|i,j| f(i,j) == true } would be the generalised method, but  
as noted that's O(n^2) rather than O(n). (Also I'm not sure it'd be  
algorithmically sound - you could introduce order dependencies with a  
sufficiently adversarial comparison function.)

martin

> **···**
>
> itsme213 \<itsme213@hotmail.com\> wrote:
> 
> > "Martin DeMello" \<martindemello@yahoo.com\> wrote in message
> > 
> > \> A uniq\_by would probably cover  
> > \> most of the use cases anyway.
> > 
> > Why a different method? Is that the way it is done elsewhere? Why not  
> > def uniq  
> > &nbsp;&nbsp;&nbsp;&nbsp;if block\_given?  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;... less\_efficient  
> > &nbsp;&nbsp;&nbsp;&nbsp;else default\_way  
> > end

---

<div class="post-metadata">

**Author:** ![ts1](https://avatars.discourse-cdn.com/v4/letter/t/a8b319/32.png) [@ts1](https://rubytalk.org/u/ts1)\
**Post date:** [27 January 2005 12:39 UTC](https://rubytalk.org/t/array-uniq-block/16048/14 "2005-01-27T12:39:39Z")

</div>

> Of all what you said I don't understand your last point: Why is there a  
> collision?

Array#uniq just store the element of the array as the key of a hash.

For an hash, 2 values are stored in differents keys if they have a  
different hash value, or if eql? return false.

In hash "terminology", there is collision when 2 elements have the same hash  
value but are different (eql? return false) (see st.c)

Guy Decoux

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 January 2005 10:45 UTC](https://rubytalk.org/t/array-uniq-block/16048/15 "2005-01-30T10:45:49Z")

</div>

> By analogy with sort and sort\_by

> a.uniq\_by {|i| i[1]} would work like uniq, but hashing i[i]  
> rather than i. a.uniq {|i,j| f(i,j) == true } would be the  
> generalised method, but as noted that's O(n^2) rather  
> than O(n). (Also I'm not sure it'd be algorithmically sound  
> - you could introduce order dependencies with a sufficiently  
> adversarial comparison function.)

Has anyone, could anyone, write a Ruby version of this method? I'm not  
sure what its supposed to do exaclty. Id like to see source.  
Thanks,  
T.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [27 January 2005 12:50 UTC](https://rubytalk.org/t/array-uniq-block/16048/16 "2005-01-27T12:50:54Z")

</div>

"ts" \<decoux@moulon.inra.fr\> schrieb im Newsbeitrag  
news:200501271239.j0RCdOg11318@moulon.inra.fr...

> \> Of all what you said I don't understand your last point: Why is there

a

> \> collision?
> 
> Array#uniq just store the element of the array as the key of a hash.
> 
> For an hash, 2 values are stored in differents keys if they have a

I think the term is "bucket" and not "key". Maybe that was confusing me.

> different hash value, or if eql? return false.
> 
> In hash "terminology", there is collision when 2 elements have the same

hash

> value but are different (eql? return false) (see st.c)

Ok, I now see what "collision" you meant. Of course there is a collision  
but still both keys are stored.

Thx for clarifying!

&nbsp;&nbsp;&nbsp;&nbsp;robert

> **···**
>
> > Guy Decoux

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [30 January 2005 11:07 UTC](https://rubytalk.org/t/array-uniq-block/16048/17 "2005-01-30T11:07:48Z")

</div>

Trans schrieb:

> Has anyone, could anyone, write a Ruby version of this method? I'm not  
> sure what its supposed to do exaclty. Id like to see source.

No problem:

&nbsp;&nbsp;&nbsp;require 'set'

&nbsp;&nbsp;&nbsp;class Array  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def uniq\_by  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result =   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = Set.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |elem|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield elem  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless values.include? value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values \<\< value  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< elem  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;p ( 0 .. 9 ).to\_a.uniq\_by { |x| x % 4 } # =\> [0, 1, 2, 3]

I bet Robert will transform this into a version using inject 😉

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Martin\_DeMello1](https://avatars.discourse-cdn.com/v4/letter/m/3be4f8/32.png) [@Martin\_DeMello1](https://rubytalk.org/u/Martin_DeMello1)\
**Post date:** [30 January 2005 11:25 UTC](https://rubytalk.org/t/array-uniq-block/16048/18 "2005-01-30T11:25:48Z")

</div>

module Enumerable  
&nbsp;&nbsp;def uniq\_by(\*args, &blk)  
&nbsp;&nbsp;&nbsp;&nbsp;blk = lambda {|i| i.send(\*args)} unless args.empty?  
&nbsp;&nbsp;&nbsp;&nbsp;raise "needs args or block" unless blk  
&nbsp;&nbsp;&nbsp;&nbsp;h = {}  
&nbsp;&nbsp;&nbsp;&nbsp;a =   
&nbsp;&nbsp;&nbsp;&nbsp;each {|i|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;k = blk.call(i)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a \<\< i unless h[k]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;h[k] = true  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;a  
&nbsp;&nbsp;end  
end

p (-5..5).to\_a.uniq\_by {|i| i\*i}  
p [[1, 2], [3, 4], [5, 6], [7, 2]].uniq\_by(:at, 1)

martin

> **···**
>
> Trans \<transfire@gmail.com\> wrote:
> 
> > Has anyone, could anyone, write a Ruby version of this method? I'm not  
> > sure what its supposed to do exaclty. Id like to see source.

---

<div class="post-metadata">

**Author:** ![7rans](https://avatars.discourse-cdn.com/v4/letter/7/a8b319/32.png) [@7rans](https://rubytalk.org/u/7rans)\
**Post date:** [30 January 2005 13:55 UTC](https://rubytalk.org/t/array-uniq-block/16048/19 "2005-01-30T13:55:48Z")

</div>

LOL 🙂

\*[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/128868](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/128868)  
Oh, that's rich. I think I need a vacation.

---

<div class="post-metadata">

**Author:** ![Florian\_Gross](https://avatars.discourse-cdn.com/v4/letter/f/e9a140/32.png) [@Florian\_Gross](https://rubytalk.org/u/Florian_Gross)\
**Post date:** [30 January 2005 14:10 UTC](https://rubytalk.org/t/array-uniq-block/16048/20 "2005-01-30T14:10:54Z")

</div>

Pit Capitain wrote:

> Trans schrieb:
> 
> > Has anyone, could anyone, write a Ruby version of this method? I'm not  
> > sure what its supposed to do exaclty. Id like to see source.
> 
> No problem:
> 
> &nbsp;&nbsp;require 'set'
> 
> &nbsp;&nbsp;class Array  
> &nbsp;&nbsp;&nbsp;&nbsp;def uniq\_by  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result =   
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values = Set.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;each do |elem|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield elem  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless values.include? value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;values \<\< value  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result \<\< elem  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;result  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;p ( 0 .. 9 ).to\_a.uniq\_by { |x| x % 4 } # =\> [0, 1, 2, 3]
> 
> I bet Robert will transform this into a version using inject 😉

module Enumerable  
&nbsp;&nbsp;&nbsp;def uniq\_by()  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inject() do |state, item|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;value = yield(item)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state.include?(value) ? state : state + [item]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

[Next page](https://rubytalk.org/t/array-uniq-block/16048.md?page=2)
