A set object in Ruby...

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

Hey, is there a Set object, like an Array or Hash object, in Ruby?
So far, I've been using the Hash object and mapped the key to true.
Then, I use the .include? method to determine if my "set" has the object,
already. However, while reading a tutorial on Python, I noticed that it has
a Set data type, which made me wonder if Ruby has one as well and, if not,
why not?
Thanks...
 
K

Kenosis

No, there's no set class in Ruby proper. There exists a finite set
class in the RAA (http://raa.ruby-lang.org/project/finiteset/) but I've
not investigated its implementation. A set based on a hash as you've
done seems reasonable to me, for potentially large sets. Another way
to go, which may not be as efficient if you get a lot of duplicates,
would be to add the items blindly to an array and then use array.uniq!
to enforce set member uniqueness.

Ken
 
K

Kenosis

Doh! Grabbed the wrong edition of my Ruby manual. Oh well, look TWICE
before you leap and check ri to be sure, comes to mind :)

Ken

Jeffrey said:
Just said:
Hey, is there a Set object, like an Array or Hash object, in Ruby?
So far, I've been using the Hash object and mapped the key to true.
Then, I use the .include? method to determine if my "set" has the object,
already. However, while reading a tutorial on Python, I noticed that it has
a Set data type, which made me wonder if Ruby has one as well and, if not,
why not?
Thanks...

irb(main):001:0> require 'set'
=> true
irb(main):002:0> s = Set.new
=> #<Set: {}>
irb(main):003:0> s << 1
=> #<Set: {1}>
irb(main):004:0> s << "hello"
=> #<Set: {1, "hello"}>
irb(main):005:0> s << [2, 3, 5, 7]
=> #<Set: {1, "hello", [2, 3, 5, 7]}>
irb(main):006:0> s
=> #<Set: {1, "hello", [2, 3, 5, 7]}>
irb(main):007:0>

Use "ri Set" for details.
 
J

Jens Auer

Jeffrey said:
irb(main):001:0> require 'set'
=> true
irb(main):002:0> s = Set.new
=> #<Set: {}>
irb(main):003:0> s << 1
=> #<Set: {1}>
irb(main):004:0> s << "hello"
=> #<Set: {1, "hello"}>
irb(main):005:0> s << [2, 3, 5, 7]
=> #<Set: {1, "hello", [2, 3, 5, 7]}>
irb(main):006:0> s
=> #<Set: {1, "hello", [2, 3, 5, 7]}>
irb(main):007:0>

Use "ri Set" for details.

If you want real sets in a mathematical sense, the type Set is not what
you are looking for, Csaba Henk shows in the following thread:
http://groups.google.de/group/comp....tvc=1&q=set+extensional+ruby#0956a958df6727f2
(Does anybody how I can get google-groups to show me the message id?)

If you want just a data structure with unique elements, the Set is what
you are looking for. I think the class Set should be re-named Bag, and a
new class with the mathematical correct behaviour added instead.
 
J

Just Another Victim of the Ambient Morality

Jens Auer said:
Jeffrey Schwab wrote:
http://groups.google.de/group/comp....tvc=1&q=set+extensional+ruby#0956a958df6727f2
(Does anybody how I can get google-groups to show me the message id?)

If you want just a data structure with unique elements, the Set is what
you are looking for. I think the class Set should be re-named Bag, and a
new class with the mathematical correct behaviour added instead.

I don't know how to get the message ID from google groups.
This is amazing. I don't think I've ever seen an honest-to-goodness bug
in Ruby before but this is definitely an error. Is anyone looking into
fixing this?
I have a rival pythonista who's really going to enjoy this...
 
J

Jens Auer

Just said:
I don't know how to get the message ID from google groups.
This is amazing. I don't think I've ever seen an honest-to-goodness bug
in Ruby before but this is definitely an error. Is anyone looking into
fixing this?
I have a rival pythonista who's really going to enjoy this...
I just checked python's behavior on this, and it seems they got it right:
from sets import ImmutableSet
singleton = ImmutableSet( [ImmutableSet([])] )
s = ImmutableSet( [ImmutableSet([]), ImmutableSet([])] )
s ImmutableSet([ImmutableSet([])])
singleton ImmutableSet([ImmutableSet([])])
s == singleton
True

So, my solution would be to keep the old Ruby Set class, but rename it
to Bag and introduce a new immutable Set class which models a
mathematical set.
 

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,065
Members
47,672
Latest member
svaraho

Latest Threads

Top