Home » Ruby programming

Ruby Set Comparison

Set comparison in Ruby: Here, we are going to learn about the set comparison in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on October 11, 2019

Set comparison in Ruby

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.

== 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

< 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

> 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

.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

.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.