ruby 类方法与实例方法_Ruby Set相交? 实例方法
ruby 類方法與實例方法
Ruby Set相交? 方法 (Ruby Set intersect? Method)
intersect?(Set) method is a method which is predefined in Ruby's library. You may call this method as an exact opposite of Set.disjoint?() method. With the help of this method, we can check whether the two sets are having any common elements. If the sets which we are comparing are having any of the common elements then the method will return true and false in the case only when there are no common elements in both the sets. This method may provide you various advantages while programming with Ruby. Let us see its syntax and example for having a better understanding of how this method is implemented in the Ruby code.
intersect?(Set)方法是在Ruby的庫中預定義的方法。 您可以將此方法稱為Set.disjoint?()方法的完全相反的方法 。 借助此方法,我們可以檢查這兩個集合是否具有任何公共元素。 如果我們正在比較的集合具有任何公共元素,則僅當兩個集合中都沒有公共元素時,該方法才會返回true和false。 使用Ruby編程時,此方法可能為您提供各種優勢。 讓我們看一下它的語法和示例,以更好地了解如何在Ruby代碼中實現此方法。
Syntax:
句法:
Set.intersect?(Set)Example 1:
范例1:
=begin Ruby program to demonstrate the implementation of intersect?() method. =endrequire 'set'Vegetable=Set.new(["potato", "brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])p = Vegetable.intersect?(Fruits)if p == falseputs "There is no common element exists between both the sets." elseputs "The sets are having common elements." endOutput
輸出量
There is no common element exists between both the sets.Explanation:
說明:
In the above code, we have declared two instances of Set class known as Vegetable and Fruits. We want to check whether there exist some common elements between both the sets or not. We are proceeding with the help of the intersect? method. We know that it returns a Boolean value. So, we are storing its value inside a variable. We are then checking the value of that variable, if it is false then it simply means that there are no common elements between both the sets. This method will give you result as true even if there is only one element that is common between both the instances of Set class.
在上面的代碼中,我們聲明了Set類的兩個實例,這些實例稱為Vegetable和Fruits 。 我們要檢查這兩個集合之間是否存在某些公共元素。 我們在相交的幫助下進行嗎? 方法 。 我們知道它返回一個布爾值。 因此,我們將其值存儲在變量中。 然后,我們正在檢查該變量的值,如果它為false,則僅表示這兩個集合之間沒有公共元素。 即使Set類的兩個實例之間只有一個公共元素,此方法也會為您提供真實的結果。
Example 2:
范例2:
=begin Ruby program to show the implementation of intersect?() . =endrequire 'set'p = Set[2,3,5].intersect?Set[2,56,4,3,22,66,34]if p == falseputs "There is no common element exists between both the sets." elseputs "The sets are having common elements." endOutput
輸出量
The sets are having common elements.Explanation:
說明:
In the above code, we are creating sets at the time of invoking the intersect? function. The function intersect?() is checking whether both the sets are having some common elements or not. If the sets are having even a single common element then it will return true. We are storing its returned value inside a variable ‘p’. We are checking the value of p, if it is having false then it means that there are some common elements inside both the sets. We are informing the user about this with the help of puts statements.
在上面的代碼中,我們在調用相交時創建了集合? 功能 。 函數intersect?()正在檢查這兩個集合是否都具有某些公共元素。 如果集合甚至具有單個公共元素,則它將返回true。 我們將其返回值存儲在變量“ p”中。 我們正在檢查p的值,如果p的值為false,則表示這兩個集合內都有一些公共元素。 我們正在puts語句的幫助下通知用戶。
翻譯自: https://www.includehelp.com/ruby/set-intersect-method-with-example.aspx
ruby 類方法與實例方法
總結
以上是生活随笔為你收集整理的ruby 类方法与实例方法_Ruby Set相交? 实例方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何使用JavaScript访问对象的键
- 下一篇: 卡诺模板_无关条件的卡诺地图