What is the set.proper_superset?() method in Ruby?

Overview

A set is an ordered collection of unique items that cannot be duplicated. The set A can be said to be a proper set of set B. This can happen if set A contains all the elements or items of set B but with at least one or more that is not in set B.

Syntax

set1.proper_superset(set2)
Syntax for the proper_superset?() method in Ruby

Parameters

set1: This is the set we want to check if it is a proper superset of another set, set2.

set2: This is the other set that we want to check with set set1.

Return value

The value returned is a Boolean value. If the set set1 is a proper superset of the set set2 , then a true is returned. Otherwise, it returns false.

Code example

# require the set class
require "set"
# create some sets
EvenNumbers = Set.new([2, 4, 6, 8, 10])
NumbersToTen = Set.new([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Names = Set.new(["Amaka", "Titi"])
Workers = Set.new(["Amaka", "Chioma", "Titi"])
EmptySet = Set.new([])
Alphabets = Set.new(["a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q",
"r", "s", "t", "u", "v", "w", "x", "y", "z"
])
Vowels = Set.new(["a", "e", "i", "o", "u"])
# check if proper supersets exists
puts NumbersToTen.proper_superset?(EvenNumbers) # true
puts Names.proper_superset?(Workers) # false
puts EmptySet.proper_superset?(Names) # false
puts Alphabets.proper_superset?(Vowels) # true

Explanation

  • Line 2: We require the set class.
  • Lines 5–14: We create some sets (EvenNumbers, NumbersToTen, Names, Workers, EmptySet, Alphabets, Vowels).
  • Lines 17–20: We check if some proper supersets exist using the proper_superset?() method and print the results to the console screen.

Free Resources