Set.new vs Set[]

D

Daniel Berger

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
 
D

Damphyr

Daniel said:
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.braveworld.net/riva

____________________________________________________________________
http://www.freemail.gr - äùñåÜí õðçñåóßá çëåêôñïíéêïý ôá÷õäñïìåßïõ.
http://www.freemail.gr - free email service for the Greek-speaking.
 
B

Brian Schröder

Hi all,

Ruby 1.8.3

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

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

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

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

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

Regards,

Dan

bschroed@black:~$ irb -r set

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

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

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

irb(main):004:0> Set[*a]
=3D> #<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
 

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,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top