To check if a set contains all the items of another set in Ruby, you can use the subset?
method.
In this example,
setA
and setB
containing some elements.subset?
method of setA
to check if all elements of setB
are also present in setA
.subset?
method returns a boolean value: true
if setA
is a subset of setB
, and false
otherwise.setA
is a subset of setB
.require 'set'
setA = Set.new([1, 2, 3, 4, 5])
setB = Set.new([2, 3])
is_subset = setA.subset?(setB)
puts 'Is setA a subset of setB? ' + is_subset.to_s
Is setA a subset of setB? false
In this example,
setA
and setB
containing some elements.superset?
method of setA
to check if all elements of setB
are also present in setA
.superset?
method returns a boolean value: true
if setA
is a superset of setB
, and false
otherwise.setA
is a superset of setB
.require 'set'
setA = Set.new([1, 2, 3, 4, 5])
setB = Set.new([2, 3])
is_superset = setA.superset?(setB)
puts 'Is setA a superset of setB? ' + is_superset.to_s
Is setA a superset of setB? true
In this tutorial, we learned How to check if a Set contains all the items of Another Set in Ruby language with well detailed examples.