Ruby Set Set disjoint?实例方法

Ruby Set disjoint?方法

Disjoint?(Set)方法是在Ruby的库中预定义的。借助此方法,我们可以检查这两个集合是否具有任何公共元素。如果我们正在比较的集合具有任何公共元素,则该方法将返回false,并且仅在两个集合中都没有公共元素的情况下才为true。在使用Ruby编程时,有时可能会证明此方法非常有利。让我们看一下它的语法和示例,以更好地了解如何在Ruby代码中实现此方法。

语法:

    Set.disjoint?(Set)

范例1:

=begin
Ruby program to demonstrate the 
implementation of disjoint?() method.
=end

require 'set'

Vegetable=Set.new(["potato", "brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])
Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

p = Vegetable.disjoint?(Fruits)

if p == true
	puts "There is no common element exists between both the sets."
else
	puts "The sets are having common elements."
end

输出结果

There is no common element exists between both the sets.

说明:

Disjoint?(Set)方法是在Ruby的库中预定义的。 借助此方法,我们可以检查这两个集合是否具有任何公共元素。 如果我们正在比较的集合具有任何公共元素,则该方法将返回false,并且仅在两个集合中都没有公共元素的情况下才为true。 在使用Ruby编程时,有时可能会证明此方法非常有利。 让我们看一下它的语法和示例,以更好地了解如何在Ruby代码中实现此方法。

范例2:

=begin
Ruby program to show the implementation of disjoint?() .
=end

require 'set'

p = Set[2,3,5].disjoint?Set[2,56,4,3,22,66,34]

if p == true
	puts "There is no common element exists between both the sets."
else
	puts "The sets are having common elements."
end

输出结果

The sets are having common elements.

说明:

在上面的代码中,我们在调用不相交点时创建了集合。 功能。 函数disjoint?()正在检查这两个集合是否都具有一些公共元素。 如果集合甚至只有一个公共元素,则它将返回true。 我们将其返回值存储在变量“ p”中。 我们正在检查p的值,如果p的值为true,则表示这两个集合内都有一些公共元素。