TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Ruby Set Examples

This Ruby article uses the set collection. It adds elements to sets and uses many other set methods.

Sets. These have only unique elements.

Like a hash, a set provides fast lookups. But it stores no values (no pairs). It only stores keys.

Logical methods. Sets provide many logical methods for adding, removing and testing elements. We can use operations like subset for easy analysis.

First example. This example creates a new Set with the Set.new syntax. The argument to new() is an array of two elements. We then call the add() method twice.

Duplicates: One string, "socrates", is added but already exists. The duplicate is not stored.

Size: We call the size() method on the set, which returns 3. The set has three elements because the duplicate was not retained.

Include: We finally invoke the "include" method. This returns true because the argument exists within the set.

Based on:

Ruby 2

Ruby program that uses Set

require "set"

# Create new Set.
s = Set.new(["plato", "socrates"])

# Add two more elements.
s.add("cebes")
s.add("socrates")

# Get size.
puts s.size()

# See if Set includes this element.
puts s.include?("socrates")

Output

3
true

Hashing. When we call "include?" on the set, a hashed lookup occurs. In large sets, this is much faster than a linear search through the elements.

Tip: In a hashed lookup, a special code is computed that indicates a position in memory.

Tip 2: This performance characteristic is also found in the hash type. A benchmark is available.

Hash

Merge, each. Next, this program uses the merge() method and the each() iterator. With merge(), we pass in a collection and the set adds all of its elements.

Each: With each, we iterate over the elements in the set. This uses the same syntax forms available on the hash and array in Ruby.

Each

Tip: This is the simplest way to loop over a set. In this example, we use the identifier n.

Ruby program that uses Set, merge, each

require "set"

# Create set.
s = Set.new(["x", "y"])

# Merge in these two elements.
s.merge(["y", "z"])

# Iterate the set.
s.each do |n|
    # Display the element.
    puts n
end

Output

x
y
z

Subset, operators. One common operation with a set is the "subset" test. We can determine if all elements from one set are contained within another. We can use <= or the "subset?" method.

Ruby program that uses subset, operators

require "set"

# Contains three elements.
set1 = Set.new [10, 20, 30]

# Is a superset of set1.
set2 = Set.new [10, 20, 30, 40, 50]

# This is true.
if set1 <= set2
    puts true
end

# This is not evaluated to true.
if set2 <= set1
    puts false
end

# This is true.
# ... Subset is the same as the <= operator.
if set1.subset?(set2)
    puts true
end

Output

true
true

A summary. A set stores just keys, not values. In this way, it is like a hash with no values. In program contexts where no values are required, this leads to clearer code.

Sets with benefits. Performance with sets is good: hashed lookups make searches faster. But the greater benefit with a set is its logical methods, like subset, which simplify code.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf