×

Ruby Tutorial

Ruby Basics

Ruby Control Statements

Ruby Methods

Ruby Classes and Methods

Ruby Arrays

Ruby Sets

Ruby Strings

Ruby Classes & Objects

Ruby Hash

Ruby Tools

Ruby Functions

Ruby Built-in Functions

Misc.

Ruby Programs

Ruby program to check a set is a subset of another set

Last Updated : December 15, 2025

Problem Solution

In this program, we will create two objects of the Set class and add items into created sets. Then we will check a set is a subset of another set using the "<=" operator.

Program/Source Code

The source code to check a set is a subset of another set is given below. The given program is compiled and executed successfully.

# Ruby program to check a set is a 
# subset of another se

require 'set';

setObj1 = Set.new();
setObj2 = Set.new();

setObj1.add(101);
setObj1.add(102);
setObj1.add(103);
setObj1.add(104);
setObj1.add(105);

setObj2.add(103);
setObj2.add(104);
setObj2.add(105);

if setObj2 <= setObj1
    puts "setObj2 is the subset of setObj1.";
else
    puts "setObj2 is not the subset of setObj1.";
end

Output

setObj2 is the subset of setObj1.

Explanation

In the above program, we imported the "set" package using the "require" statement. Then we created two objects setObj1, setObj2 of the Set class using the new() method and added items into created set using add() method of the Set class. After that, we checked set setObj2 is the subset of setObj1 using the "<=" operator and printed the appropriate message.

Ruby Set Programs »


Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.