# In Ruby, is a set just a hash without values?

**URL:** <https://www.ruby-forum.com/t/in-ruby-is-a-set-just-a-hash-without-values/89704>\
**Category:** Ruby\
**Created:** [March 29, 2006, 2:21am UTC](https://www.ruby-forum.com/t/in-ruby-is-a-set-just-a-hash-without-values/89704 "2006-03-29T02:21:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 29, 2006, 2:21am UTC](https://www.ruby-forum.com/t/in-ruby-is-a-set-just-a-hash-without-values/89704/1 "2006-03-29T02:21:04Z")

</div>

A question - is there an object in Ruby analogous to a Java set, which  
has hash-like functionality but no values?

Or should I just use a hash with no values?

Actually, what I’m _really_ trying to do is “unique-ize” an array based  
on a particular attribute of each of it’s members. My plan was to read  
the unique attribute values into a hash as they come up and compare new  
array member’s attributes against that hash.

I bet there’s a very cool way to do it 😉 - if anyone knows and would  
like to share, I am all ears.

Thanks,  
Wes

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 29, 2006, 2:25am UTC](https://www.ruby-forum.com/t/in-ruby-is-a-set-just-a-hash-without-values/89704/2 "2006-03-29T02:25:35Z")

</div>

Wes G. wrote:

> A question - is there an object in Ruby analogous to a Java set, which  
> has hash-like functionality but no values?

require ‘set’ # should do what you’re asking for

docs here: [http://ruby-doc.org/stdlib/libdoc/set/rdoc/index.html](http://ruby-doc.org/stdlib/libdoc/set/rdoc/index.html)

–Steve

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [March 29, 2006, 2:34am UTC](https://www.ruby-forum.com/t/in-ruby-is-a-set-just-a-hash-without-values/89704/3 "2006-03-29T02:34:31Z")

</div>

On Mar 29, 2006, at 2:21 AM, Wes G. wrote:

> Actually, what I’m _really_ trying to do is “unique-ize” an array  
> based  
> on a particular attribute of each of it’s members. My plan was to  
> read  
> the unique attribute values into a hash as they come up and compare  
> new  
> array member’s attributes against that hash.
> 
> I bet there’s a very cool way to do it 😉 - if anyone knows and would  
> like to share, I am all ears.

Use the Set class (or SortedSet if your collection needs to be sorted  
and mixes in Comparable).

$ ri Set  
  
The equality of each couple of elements is determined according to  
Object#eql? and Object#hash, since Set uses Hash as storage.

– Daniel
