Are there sets in Ruby?

There is a Set class in ruby. You can use it like so:

require 'set'

set = Set.new

string = "a very very long string"

string.scan(/\w+/).each do |word|
  unless set.add?( word )
    # logic here for the duplicates
  end
end

Although, I’m wondering if you would want to count the instances in that case the following example would be better:

instances = Hash.new { |h, k| h[k] = 0 }

string.scan(/\w+/).each do |word|
  instances[word] += 1
end

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)