Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantages of Set in ruby

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.

like image 425
Prashanth Avatar asked Apr 11 '16 12:04

Prashanth


People also ask

What does set do in Ruby?

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

Does Ruby have a set?

A set is a Ruby class that helps you create a list of unique items.

Is Ruby set ordered?

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

What is %W in Ruby?

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


1 Answers

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.

like image 132
peter Avatar answered Sep 21 '22 15:09

peter