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.
=end

require '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 == false
	puts "There is no common element exists between both the sets."
else
	puts "The sets are having common elements."
end

Output

输出量

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?() .
=end

require 'set'

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

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

Output

输出量

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 类方法与实例方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值