Check whether the set is empty or not in Ruby

Here, we are going to learn how to check whether the given set is an empty set or not in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on October 20, 2019

For every problem, we can identify different alternatives after identifying them. For this problem too, we have different ways to check whether the Set is empty or not. Ruby has given us various methods through which we can perform multiple tasks. Some can be used directly for solving the purpose and some we can use indirectly also. This depends upon us that how we make an effective utilisation of the method we have and how effectively our code is written.

Methods used:

  • Set.merge(): This method is used to merge two sets.
  • Set.empty?(): This method is used to check whether the set is empty or not. The return type of this method is Boolean.
  • Set.size(): This method returns the size of the set.
  • Set.each: This method is used to traverse the elements of the set.

Variables used:

  • Vegetable: This is an instance of Set class.
  • Fruits: This is an instance of Set class.
  • count: This is a counter variable which is used to count the number of elements present in the set.

Program 1:

=begin
Ruby program to find the empty set 
with Set.empty?() method.
=end

require 'set'

Vegetable = Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

Vegetable.merge(Fruits)

if Vegetable.empty?()
	puts "Set is empty"
else
	puts "Set is not empty"
end

Output

Set is not empty

Explanation:

In the above code, we are making use of Set.empty?() method which is predefined in Ruby's library. This makes the task very easy because we only have to write a single line and out task is done. The output tells that the set is not empty because set is having elements which we have added at the time of its definition as well as after merging it with another set.

Program 2:

=begin
Ruby program to find the empty set.
=end

require 'set'

Vegetable = Set.new([ "potato", "brocolli" , "broccoflower" , "lentils" , "peas" , "fennel" , "chilli" , "cabbage" ])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

Vegetable.merge(Fruits)

count = 0
Vegetable.each do |element|
    count +=1
end

if count == 0
	puts "Set is empty"
else
	puts "Set is not empty"
end

Output

Set is not empty 

Explanation:

In the above code, we are using Set.each to check whether set is empty or not. We are using a counter variable which is getting incremented inside the loop. If the counter variable remains 0 after the successful termination of loop, this means that the set contains nothing as element. We will find that the set is not empty because we have added elements to it at the time of definition.

Program 3:

=begin
Ruby program to find the empty set.
=end

require 'set'

Vegetable = Set.new([ "potato" ,  "brocolli" , "broccoflower" , "lentils" , "peas" , "fennel" , "chilli" , "cabbage" ])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

Vegetable.merge(Fruits)

if Vegetable.size() == 0
	puts "Set is empty"
else
	puts "Set is not empty"
end

Output

Set is not empty  

Explanation:

In the above code, we are using Set.size() method to check whether the set is empty or not. It is very obvious that if the size of set is 0 then it is empty otherwise not.

Ruby Set Programs »





Comments and Discussions!

Load comments ↻






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