[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Question about the Set class, XOR, and arrays

Daniel Berger

11/1/2006 8:24:00 PM

Hi all,

Ruby 1.8.5

irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
=> #<Set: {5, 1, 2, 4}>

Ok, looks good.

irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
=> #<Set: {1, 2, 4}>

What?! I'm confused. Do I need a refresher in Set theory or something?

Thanks,

Dan

6 Answers

Ryan Davis

11/1/2006 8:32:00 PM

0


On Nov 1, 2006, at 12:25 PM, Daniel Berger wrote:

> irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
> => #<Set: {5, 1, 2, 4}>
>
> Ok, looks good.
>
> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
> => #<Set: {1, 2, 4}>
>
> What?! I'm confused. Do I need a refresher in Set theory or something?

It is the way it is implemented... subsequent 5's will toggle it on
and off. You should file a bug.

Joel VanderWerf

11/1/2006 8:34:00 PM

0

Daniel Berger wrote:
> Hi all,
>
> Ruby 1.8.5
>
> irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
> => #<Set: {5, 1, 2, 4}>
>
> Ok, looks good.
>
> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
> => #<Set: {1, 2, 4}>
>
> What?! I'm confused. Do I need a refresher in Set theory or something?

I think I can guess why it is happening:

irb(main):009:0> (0..10).each {|n| p Set[1,2,3] ^ ([3,4]+[5]*n)}
#<Set: {1, 2, 4}>
#<Set: {5, 1, 2, 4}>
#<Set: {1, 2, 4}>
#<Set: {5, 1, 2, 4}>
#<Set: {1, 2, 4}>
#<Set: {5, 1, 2, 4}>
#<Set: {1, 2, 4}>
#<Set: {5, 1, 2, 4}>
#<Set: {1, 2, 4}>
#<Set: {5, 1, 2, 4}>
#<Set: {1, 2, 4}>
=> 0..10

Looks like the array argument is used to flip-flop the elements of the set.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Paul Lutus

11/1/2006 8:45:00 PM

0

Daniel Berger wrote:

> Hi all,
>
> Ruby 1.8.5
>
> irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
> => #<Set: {5, 1, 2, 4}>
>
> Ok, looks good.
>
> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
> => #<Set: {1, 2, 4}>
>
> What?! I'm confused. Do I need a refresher in Set theory or something?

Are your irb entries above copied directly from the display? Or is this a
1.8.5 problem?

$ ruby -v
$ ruby 1.8.4 (2005-12-24) [i386-linux]

$irb
irb(main):002:0> require 'set'
=> true
irb(main):003:0> Set[1,2,3] ^ Set[3,4,5,5]
=> #<Set: {5, 1, 2, 4}>
irb(main):004:0> Set[1,2,3] ^ Set[3,4,5,5]
=> #<Set: {5, 1, 2, 4}>
irb(main):005:0> Set[1,2,3] ^ Set[3,4,5,5]
=> #<Set: {5, 1, 2, 4}>

--
Paul Lutus
http://www.ara...

Daniel Berger

11/1/2006 9:27:00 PM

0

Ryan Davis wrote:
> On Nov 1, 2006, at 12:25 PM, Daniel Berger wrote:
>
> > irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
> > => #<Set: {5, 1, 2, 4}>
> >
> > Ok, looks good.
> >
> > irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
> > => #<Set: {1, 2, 4}>
> >
> > What?! I'm confused. Do I need a refresher in Set theory or something?
>
> It is the way it is implemented... subsequent 5's will toggle it on
> and off. You should file a bug.

Done.

Thanks for confirming my sanity.

- Dan

Jeff Schwab

11/2/2006 2:16:00 PM

0

Daniel Berger wrote:
> Ryan Davis wrote:
>> On Nov 1, 2006, at 12:25 PM, Daniel Berger wrote:
>>
>>> irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
>>> => #<Set: {5, 1, 2, 4}>
>>>
>>> Ok, looks good.
>>>
>>> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
>>> => #<Set: {1, 2, 4}>
>>>
>>> What?! I'm confused. Do I need a refresher in Set theory or something?
>> It is the way it is implemented... subsequent 5's will toggle it on
>> and off. You should file a bug.
>
> Done.
>
> Thanks for confirming my sanity.

In what way is this a bug? I'm not aware of set theory saying anything
about the intersection of a set with an array.

Joel VanderWerf

11/2/2006 5:44:00 PM

0

Jeffrey Schwab wrote:
> Daniel Berger wrote:
>> Ryan Davis wrote:
>>> On Nov 1, 2006, at 12:25 PM, Daniel Berger wrote:
>>>
>>>> irb(main):002:0> Set[1,2,3] ^ Set[3,4,5,5]
>>>> => #<Set: {5, 1, 2, 4}>
>>>>
>>>> Ok, looks good.
>>>>
>>>> irb(main):003:0> Set[1,2,3] ^ [3,4,5,5]
>>>> => #<Set: {1, 2, 4}>
>>>>
>>>> What?! I'm confused. Do I need a refresher in Set theory or something?
>>> It is the way it is implemented... subsequent 5's will toggle it on
>>> and off. You should file a bug.
>>
>> Done.
>>
>> Thanks for confirming my sanity.
>
> In what way is this a bug? I'm not aware of set theory saying anything
> about the intersection of a set with an array.

Arrays already behave like sets in some ways. They have set-like operators:

irb(main):009:0* [1,2,3] & [2,3,4]
=> [2, 3]
irb(main):010:0> [1,2,3] | [2,3,4]
=> [1, 2, 3, 4]
irb(main):011:0> [1,2,3] - [2,3,4]
=> [1]

But this argument falls down because arrays don't have symmetric difference:

irb(main):012:0> [1,2,3] ^ [2,3,4]
NoMethodError: undefined method `^' for [1, 2, 3]:Array
from (irb):11

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407