ruby 数组元素替换_从Ruby中的集合中删除并替换元素

本文介绍如何在 Ruby 的 Set 类中替换和删除元素,包括使用 Set.replace 和 Set.delete 方法的具体步骤,并提供示例代码。

ruby 数组元素替换

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.

Ruby具有完成特定任务的各种特定方法。 如果在Set中存在某些元素,则可能需要在几个地方从set的实例中替换或删除元素 。 您可以借助Set.delete方法执行删除操作,如果我们谈论元素替换 ,我们将从Set.replace方法中获取帮助。 在本教程中,您将找到代码,通过这些代码可以找到问题的解决方案。

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

    第一个程序将告诉您从Set中替换元素的方法。

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

    第二个程序将告诉您从Set中删除元素的方法。

Methods used:

使用的方法:

  • Set.merge: This method is used to merge two sets.

    Set.merge :此方法用于合并两个集合。

  • ===: 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.replace :借助此方法,我们可以用特定集中的新元素替换一个元素。

  • Set.delete: This method is used to delete an element from the set.

    Set.delete :此方法用于从集合中删除元素。

Variables used:

使用的变量:

  • Vegetable: This is an instance of Set class.

    Vegetable :这是Set类的实例。

  • Fruits: This is an instance of Set class.

    水果 :这是Set类的实例。

Program 1:

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

在上面的代码中,您可以观察到我们首先合并了两个set 。 然后,我们向用户询问他或她要替换的元素。 如果元素存在于集合中,则将进行替换。 我们使用===运算符来检查Set中某个元素的存在。 如果出现的元素为真,则要求用户提供新元素,并将旧元素替换为新元素。

Program 2:

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

首先在上面的代码中,我们合并了两个Set 。 我们正在向用户询问他或她要删除的元素。 首先,我们在===运算符的帮助下检查元素是否存在。 我们也可以为此使用Set.include?()方法 。 如果发现存在该元素,则可以在Set.delete方法的帮助下将其删除

翻译自: https://www.includehelp.com/ruby/delete-and-replace-an-element-from-the-set.aspx

ruby 数组元素替换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值