ruby 类方法与实例方法_Ruby Set不相交? 实例方法

ruby 类方法与实例方法

Ruby Set不相交? 方法 (Ruby Set disjoint? Method)

Disjoint?(Set) method is predefined in Ruby's library. 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 false and it will be true in the case only when there are no common elements in both the sets. This method may be proved very advantageous some of the time 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.

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

Syntax:

句法:

    Set.disjoint?(Set)

Example 1:

范例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

Output

输出量

There is no common element exists between both the sets.

Explanation:

说明:

In the above code, we have declared two instance 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 disjoint? 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 true then it simply means that there are no common elements between both the sets. This method will give you result as false even if there is only one element which is common between both the instances of Set class.

在上面的代码中,我们声明了Set类的两个实例,即Vegetable和Fruits 。 我们要检查这两个集合之间是否存在某些公共元素。 我们是在脱节的帮助下进行的吗? 方法 。 我们知道它返回一个布尔值。 因此,我们将其值存储在变量中。 然后,我们将检查该变量的值,如果为true,则仅表示这两个集合之间没有公共元素。 即使Set类的两个实例之间只有一个公共元素,此方法也会使结果为false。

Example 2:

范例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

Output

输出量

The sets are having common elements.

Explanation:

说明:

In the above code, we are creating sets at the time of invoking the disjoint? function. The function disjoint?() 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 true 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.

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

翻译自: https://www.includehelp.com/ruby/set-disjoint-method-with-example.aspx

ruby 类方法与实例方法

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值