日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起

發(fā)布時間:2025/3/11 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

ruby 集合 分組

In this program, we will see how we can combine the two sets? This is not a very difficult task. This can be easily done with the help of the + operator. In many places of programming, you will find that + operator is overloaded for various types to meet various purposes. Here in the case of sets, + operator works likes arithmetic OR. It combines all the elements from both the sets and returns a set that contains all those elements. Let us look at the code and understand how we can complete this task.

在此程序中,我們將看到如何將兩個集合結(jié)合起來 ? 這不是一個非常困難的任務(wù)。 可以在+運算符的幫助下輕松完成此操作 。 在許多編程地方,您會發(fā)現(xiàn)+運算符對于各種類型都已重載以滿足各種目的。 在集合的情況下, +運算符的作用類似于算術(shù)OR 。 它組合了兩個集合中的所有元素,并返回包含所有這些元素的集合。 讓我們看一下代碼,了解如何完成此任務(wù)。

Methods used:

使用的方法:

  • + : In ruby, most of the operators are considered as methods. This operator or method is used to combine two sets provided as arguments to the method. The return type of this operator is set itself. The set returned is having all the elements which are present in both the sets. Syntax:

    + :在Ruby中,大多數(shù)運算符被視為方法。 此運算符或方法用于合并作為方法的參數(shù)提供的兩個集合。 此運算符的返回類型是自己設(shè)置的。 返回的集合具有兩個集合中都存在的所有元素。 句法:

    SetA + SetB
  • set.each :- set.each method is used to print the elements from the set one by one. It will provide you elements in the forward direction.

    set.each:-set.each方法用于逐個打印集合中的元素。 它將為您提供前進方向的元素 。

  • Variables used:

    使用的變量:

    • Vegetable : It is an instance of Set class. It is the first argument passed as the argument in + operator.

      Vegetable :它是Set類的一個實例。 它是在+運算符中作為參數(shù)傳遞的第一個參數(shù)。

    • Sabzi : It is an instance of Set class. It is the second argument that is passed in + operator.

      Sabzi :它是Set類的實例。 這是在+運算符中傳遞的第二個參數(shù)。

    • New_set : It is containing the set which is returned from the + operator or method.

      New_set :它包含從+運算符或方法返回的集合。

    Program:

    程序:

    =begin Ruby program to show implementation of + operator =end require 'set'Vegetable=Set.new(["potato","tomato","brinjal","onion","peas","beetroot","chilli","cabbage"])Sabzi=Set.new(["potato","tomato","brinjal","onion","beetroot","capsicum","chilli"])New_set = Vegetable + SabziNew_set.each do |string|puts "#{string} element from new set" end

    Output

    輸出量

    potato element from new set tomato element from new set brinjal element from new set onion element from new set peas element from new set beetroot element from new set chilli element from new set cabbage element from new set capsicum element from new set

    Explanation:

    說明:

    In the above code, it is shown how one can combine all the elements from both the sets? As you can see above, we have defined three sets, two sets are for carrying out the processing and one set is for storing the common elements from both the sets. We have taken help from the set.each method to print all the elements from the new set. As a result, you can observe that there is no repetition of elements. It is having all the elements and each element is present only for one time. Elements are appearing single even if they are present in both sets.

    在上面的代碼中,顯示了如何組合兩個集合中的所有元素? 如上所示,我們定義了三個集合,兩個集合用于執(zhí)行處理,一個集合用于存儲兩個集合中的公共元素。 我們從set.each方法獲得幫助,以打印新集中的所有元素。 結(jié)果,您可以觀察到元素沒有重復(fù)。 它具有所有元素,每個元素僅出現(xiàn)一次。 即使元素出現(xiàn)在兩個集合中,它們也都顯示為單個。

    翻譯自: https://www.includehelp.com/ruby/join-all-the-elements-of-two-sets-in-ruby.aspx

    ruby 集合 分組

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

    總結(jié)

    以上是生活随笔為你收集整理的ruby 集合 分组_将Ruby中两个集合的所有元素结合在一起的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。