×

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 Set Comparison

By IncludeHelp Last updated : December 01, 2024

Ruby provides you various methods through which you can carry out comparison operations within two sets. Now, let us go through various methods and understand their implementation with the help of their syntax and examples.

Set comparison using == operator

This operator returns true when both the sets are equal or you can say that for the value to be true, set2 must be having the same values as of set1.

Syntax

Set1 == Set2

Example

=begin
Ruby program to show ==
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion"])

bool = Vegetable == Sabzi

if bool == true
  puts "Vegetable is equal to Sabzi"
else
  puts "Vegetable is not equal to Sabzi"
end

Output

Vegetable is equal to Sabzi

Set comparison using < operator

This operator is an alias for the proper subset. This operator will return true when Set2 is having all the elements which are present in Set1. Set2 may or may not have more elements.

Syntax

Set2 < Set1

Example

=begin
Ruby program to show < operator
=end

require 'set'

Vegetable = Set.new(["potato" , "tomato" , "brinjal" , "onion"])

Sabzi = Set.new(["potato" , "tomato" , "brinjal" , "onion" , "beetroot"])

bool = Vegetable < Sabzi

if bool == true
  puts "Vegetable is a subset of Sabzi"
else
  puts "Vegetable is not a subset to Sabzi"
end

Set comparison using > operator

This operator is an alias for a proper superset. This operator will return true when Set1 is having all the elements which are present in Set2. Set1 may or may not have more elements.

Syntax

Set2 > Set1

Example

=begin
Ruby program to show > operator
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable > Sabzi

if bool == true
  puts "Vegetable is a superset of Sabzi"
else
  puts "Vegetable is not a superset of Sabzi"
end

Output

Vegetable is not a superset of Sabzi

Set comparison using .subset?()

This operator is an alias for a proper superset. This operator will return true when Set1 is having all the elements which are present in Set2. Set1 may or may not have more elements.

Syntax

set1.subset?(set2)

Example

=begin
Ruby program to show subset method
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion","peas"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable.subset?(Sabzi)

if bool == true
  puts "Vegetable is a subset of Sabzi"
else
  puts "Vegetable is not a subset of Sabzi"
end

Output

Vegetable is not a subset of Sabzi

Set comparison using .superset?()

The working of this method is the same as of < operator as > operator is an alias for this method. This method is implemented in the following manner:

Syntax

set1.superset?(set2)

Example

=begin
Ruby program to show superset method
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion","peas","beetroot"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot"])

bool = Vegetable.superset?(Sabzi)

if bool == true
  puts "Vegetable is a superset of Sabzi"
else
  puts "Vegetable is not a superset of Sabzi"
end

Output

Vegetable is a superset of Sabzi

Comments and Discussions!

Load comments ↻





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