What is the difference between Set
and Array
in Ruby except for the fact that sets keep unique elements while arrays can keep duplicate elements?
What is the difference between Set
and Array
in Ruby except for the fact that sets keep unique elements while arrays can keep duplicate elements?
They are very different.
a[3]
references the 4th object in the array.[1, 'apple', String, 1, :banana]
(this creates and initializes a new Array).Set
is not part of the core, but part of the standard library, and thus needs a require 'set'
.Set.new
.
Set[]
(e.g. Set[1,2,3]
)