Delete and replace an element from the set in Ruby

Here, we are going to learn how to delete and replace an element from the given set in Ruby programming language?
Submitted by Hrithik Chandra Prasad, on October 24, 2019

Ruby has various specific methods to fulfil specific tasks. At several places, you may need to replace or delete an element from an instance of set class provided that there are certain elements present in the Set. You can perform deletion operation with the help of Set.delete method and if we talk about the replacement of an element, we take help from Set.replace method. In this tutorial, you will find codes through which you can find the solutions to the problem.

  • The first program will tell you the way to replace an element from the Set.
  • The second program will tell you the way to delete an element from the Set.

Methods used:

  • Set.merge: This method is used to merge two sets.
  • ===: With the help of this operator we can check the presence of an element in the set.
  • Set.replace: With the help of this method, we can replace an element with a new element in the specific set.
  • Set.delete: This method is used to delete an element from the set.

Variables used:

  • Vegetable: This is an instance of Set class.
  • Fruits: This is an instance of Set class.

Program 1:

=begin
Ruby program to replace an element from the 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)

puts "Enter the element you want to replace: "
element = gets.chomp

if Vegetable === element
  puts "Enter the new element"
  newele = gets.chomp
	if Vegetable.replace([element,newele])
  	puts "element Replaced"
  else
    puts "Error"
  end
else
	puts "element not present"
end

Output

RUN 1:
Enter the element you want to replace: 
 Mango
Enter the new element
 Jackfruit
element Replaced

RUN 2:
Enter the element you want to replace: 
 beetroot
element not present

Explanation:

In the above code, you can observe that we are first merging two sets. We are then asking the user for the element which he or she wants to replace. If the element is present in the set then the replacement will take place. We are using === operator to check the presence of an element in the Set. If the presence of element comes out to be true then the user is asked for the new element and the old element is replaced with the new element.

Program 2:

=begin
Ruby program to delete an element from the 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)

puts "Enter the element you want to delete: "
element = gets.chomp
if Vegetable === element
	Vegetable.delete(element)
	puts "element deleted"
else
	puts "element not present"
end

Output

RUN 1:
Enter the element you want to delete: 
 Apple
element deleted

RUN 2:
Enter the element you want to delete: 
 Jackfruit
element not present

Explanation:

In the above code first, we have merged two Sets. We are asking the user for the element which he or she wants to delete. First, we are checking whether the element is present or not with the help of === operator. We can also use Set.include?() method for this purpose. If we find that element is present, we delete it with the help of the Set.delete method.

Ruby Set Programs »




Comments and Discussions!

Load comments ↻





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