Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sets in Ruby?

I need a collection that is like a set. Basically I'm scanning a long string and adding words to the collection but I want to be able to detect when there are duplicates.

If sets aren't available, what's the most efficient way of doing this in Ruby? Brownie points for example code.

like image 254
alamodey Avatar asked Mar 02 '09 11:03

alamodey


People also ask

What are sets in Ruby?

What is a Ruby set? A set is a class that stores items like an array… But with some special attributes that make it 10x faster in specific situations!

Are Ruby sets ordered?

A Quick Intro to Ruby's Set (3 Part Series) All objects in a Set are guaranteed unique. Objects in a Set are not ordered. Sets are built on top of Hashes for super-fast object lookup.

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.

How do you add to an array in Ruby?

unshift will add a new item to the beginning of an array. With insert you can add a new element to an array at any position.


2 Answers

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 
like image 114
ucron Avatar answered Sep 20 '22 03:09

ucron


From the documentation:

a = [ "a", "a", "b", "b", "c" ] a.uniq  #gets you   ["a", "b", "c"] a.uniq.uniq! #gets you nil (no duplicates :) 
like image 29
dirkgently Avatar answered Sep 20 '22 03:09

dirkgently