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

W

Wes Gamble

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
 
D

Daniel Harple

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
<snip>
The equality of each couple of elements is determined according to
Object#eql? and Object#hash, since Set uses Hash as storage.

-- Daniel
 

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
474,204
Messages
2,571,062
Members
47,669
Latest member
johnmaxwell

Latest Threads

Top