日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

發布時間:2025/3/11 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 数组元素替换_从Ruby中的集合中删除并替换元素 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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. =endrequire '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.chompif Vegetable === elementputs "Enter the new element"newele = gets.chompif Vegetable.replace([element,newele])puts "element Replaced"elseputs "Error"end elseputs "element not present" end

Output

輸出量

RUN 1: Enter the element you want to replace: Mango Enter the new elementJackfruit element ReplacedRUN 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. =endrequire '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 === elementVegetable.delete(element)puts "element deleted" elseputs "element not present" end

Output

輸出量

RUN 1: Enter the element you want to delete: Apple element deletedRUN 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 數組元素替換

總結

以上是生活随笔為你收集整理的ruby 数组元素替换_从Ruby中的集合中删除并替换元素的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。