[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

A set object in Ruby...

Just Another Victim of the Ambient Morality

6/27/2006 9:05:00 PM

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



7 Answers

Kenosis

6/27/2006 9:48:00 PM

0

No, there's no set class in Ruby proper. There exists a finite set
class in the RAA (http://raa.ruby-lang.org/project/...) 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

Just Another Victim of the Ambient Morality wrote:
> 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...

Jeff Schwab

6/27/2006 9:54:00 PM

0

Just Another Victim of the Ambient Morality wrote:
> 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.

Kenosis

6/27/2006 10:00:00 PM

0

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 Schwab wrote:
> Just Another Victim of the Ambient Morality wrote:
> > 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.

Jeff Schwab

6/27/2006 11:55:00 PM

0

Kenosis wrote:
> 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

Honestly, I end up using hashes instead of proper sets a lot, anyway.
It's just the habit I picked up from years of Perl.

>
> Jeffrey Schwab wrote:
>> Just Another Victim of the Ambient Morality wrote:
>>> 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.
>

Jens Auer

6/28/2006 8:06:00 AM

0

Jeffrey Schwab wrote:

> 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.lang.ruby/browse_frm/thread/9e2c3f9e5c10d860/0956a958df6727f2?tvc=1&q=set+extensional+ruby#0956a9...
(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.

Just Another Victim of the Ambient Morality

6/28/2006 6:46:00 PM

0


"Jens Auer" <jens.auer-news@betaversion.net> wrote in message
news:e7tdfv$sue$1@f1node01.rhrz.uni-bonn.de...
> Jeffrey Schwab wrote:
> http://groups.google.de/group/comp.lang.ruby/browse_frm/thread/9e2c3f9e5c10d860/0956a958df6727f2?tvc=1&q=set+extensional+ruby#0956a9...
> (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...


Jens Auer

6/29/2006 8:22:00 AM

0

Just Another Victim of the Ambient Morality wrote:

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