The main advantage of Set
seems to be maintaining unique elements. But that can be easily achieved in Array
with,
array = [2,3,4]
array | [2,5,6] # => [2,3,4,5,6]
The only distinct feature (which could apply to few use-cases) I came across was,
set1 = [1,2,3].to_set
set2 = [2,1,3].to_set
set1 == set2 # => true
[1,2,3] == [2,1,3] # => false
Since Array
has various functions and operations associated with it, when and why should I use Set
?
There are many links that compare Array
and Set
but I haven't come across significant application of Set
.
Set implements a collection of unordered values with no duplicates. This is a hybrid of Array's intuitive inter-operation facilities and Hash's fast lookup. Set is easy to use with Enumerable objects (implementing each ).
A set is a Ruby class that helps you create a list of unique items.
Because the Ruby 1.9 Set library happens to be built upon a Hash currently, you can currently use Set as an ordered set. (For example, the to_a method's implementation is just @hash. keys .)
%w(foo bar) is a shortcut for ["foo", "bar"] . Meaning it's a notation to write an array of strings separated by spaces instead of commas and without quotes around them. You can find a list of ways of writing literals in zenspider's quickref.
For the obvious reasons, see the other answers here. For performance reasons, see the result of this little benchmark in MRI Ruby 1.9.3:
require 'benchmark'
require 'set'
array = (1..100000).to_a
set = array.to_set
#hash = Hash[array.map {|x| [x, nil]}] #beter voor heel grote volumes mar trager
hash = Hash[*array]
Benchmark.bmbm do |x|
x.report("Set.include?") { 10000.times { set.include?(99999) } }
x.report("Array.include?") { 10000.times { array.include?(99999) } }
x.report("Hash.include?") { 10000.times { hash.include?(99999) } }
end
which gives
Rehearsal --------------------------------------------------
Set.include? 0.000000 0.000000 0.000000 ( 0.015604)
Array.include? 37.940000 0.000000 37.940000 ( 38.651992)
Hash.include? 0.000000 0.000000 0.000000 ( 0.001000)
---------------------------------------- total: 37.940000sec
user system total real
Set.include? 0.000000 0.000000 0.000000 ( 0.002001)
Array.include? 38.157000 0.000000 38.157000 ( 38.730615)
Hash.include? 0.000000 0.000000 0.000000 ( 0.001001)
Enough reason to use Set
or Hash
when possible.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With