# Set.new vs Set[]

Hi all,

Ruby 1.8.3

irb(main):001:0> require "set"
=> true

irb(main):002:0> a = [0,1,2,3,4,5,2,3]
=> [0, 1, 2, 3, 4, 5, 2, 3]

irb(main):003:0> set1 = Set.new(a)
=> #<Set: {5, 0, 1, 2, 3, 4}>

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

Shouldn't the set returned by Set[] also be only the unique values?

Regards,

Dan

Daniel Berger wrote:

Hi all,

Ruby 1.8.3

irb(main):001:0> require "set"
=> true

irb(main):002:0> a = [0,1,2,3,4,5,2,3]
=> [0, 1, 2, 3, 4, 5, 2, 3]

irb(main):003:0> set1 = Set.new(a)
=> #<Set: {5, 0, 1, 2, 3, 4}>

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

Shouldn't the set returned by Set also be only the unique values?

Nope, you just made a Set with an array as an element in the second example.
In the first one you have the elements of the array inserted in the set, not the array itself.
Cheers,
V.-

···

____________________________________________________________________
http://www.freemail.gr - Ã¤Ã¹Ã±Ã¥ÃÃ­ ÃµÃ°Ã§Ã±Ã¥Ã³ÃÃ¡ Ã§Ã«Ã¥ÃªÃ´Ã±Ã¯Ã­Ã©ÃªÃ¯Ã½ Ã´Ã¡Ã·ÃµÃ¤Ã±Ã¯Ã¬Ã¥ÃÃ¯Ãµ.
http://www.freemail.gr - free email service for the Greek-speaking.

bschroed@black:~\$ irb -r set

irb(main):001:0> a = [0,1,2,3,4,5,2,3]
=> [0, 1, 2, 3, 4, 5, 2, 3]

irb(main):002:0> Set.new(a)
=> #<Set: {5, 0, 1, 2, 3, 4}>

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

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

The third line is a set containing one array, the fourth line is a set
with the elements of an array.

hth,

Brian

···

On 15/10/05, Daniel Berger <djberg96@gmail.com> wrote:

Hi all,

Ruby 1.8.3

irb(main):001:0> require "set"
=> true

irb(main):002:0> a = [0,1,2,3,4,5,2,3]
=> [0, 1, 2, 3, 4, 5, 2, 3]

irb(main):003:0> set1 = Set.new(a)
=> #<Set: {5, 0, 1, 2, 3, 4}>

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

Shouldn't the set returned by Set also be only the unique values?

Regards,

Dan

Stringed instrument chords: http://chordlist.brian-schroeder.de/