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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544126.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面渣逆袭:MyBatis连环20问,这谁顶得住?

大家好,今天我们的主角是MyBatis,作为当前国内最流行的ORM框架,是我们这些crud选手最趁手的工具,赶紧来看看面试都会问哪些问题吧。基础1.说说什么是MyBatis?MyBatis logo先吹一下:Mybatis 是一个半 ORM(对…

Wi-Fi 协议和数率?

IEEE 802.11Wi-Fi 协议摘要 协议 频率 信号 最大数据速率 传统 802.11 2.4GHz FHSS 或 DSSS 2Mbps 802.11A 5GHz OFDM 54Mbps 802.11B 2.4GHz 人力资源DSSS 11Mbps 802.11G 2.4GHz OFDM 54Mbps 802.11n 2.4 或 5GHz OFDM 600Mbps(理论值) 802.11AC 5…

as_hash ruby_Ruby中带有示例的Hash.keep_if方法

as_hash rubyHash.keep_if方法 (Hash.keep_if Method) In this article, we will study about Hash.keep_if Method. The working of this method can be predicted with the help of its name but it is not as simple as it seems. Well, we will understand this method wit…

高并发下如何防重?

前言最近测试给我提了一个bug,说我之前提供的一个批量复制商品的接口,产生了重复的商品数据。追查原因之后发现,这个事情没想象中简单,可以说一波多折。1. 需求产品有个需求:用户选择一些品牌,点击确定按钮…

Virtualbox中win7虚拟机中U盘不可用问题的解决

Virtualbox版本是5.0.0,主机运行多是Ubuntu12.04 LTS,虚拟机是Win7 X64。起初Win7正常运行,Virtualbox的增强功能已安装。下面是如何一步一步解决U盘不可用的详细过程。 1. 直接插入U盘,Win7下无任何反映 Virtualbox管理器中&…

面试突击55:delete、drop、truncate有什么区别?

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 MySQL 中,删除的方法总共有 3 种:delete、truncate、drop,而三者的用法和使用…

python中八进制_在Python中以八进制格式输入数字

python中八进制Syntax to convert octal value to an integer (decimal format), 将八进制值转换为整数(十进制格式)的语法, int(oct_value, 8)Here, 这里, oct_value should contain the valid octal value oct_value应该包含有效的八进制值 8 is the…

Ruby file操作cheatsheet

每次都要查,真是蛋疼,不如一次性总结一下,以后再不记得就来这里找好了。以下代码中需要用到的文件名:filename ‘testfile.txt’ 读取其中的全部内容:File.read(filename)将一个字符串一次性写入这个文件:…

大厂也在用的 6种 数据脱敏方案,别做泄密内鬼

最近连着几天晚上在家总是接到一些奇奇怪怪的电话,“哥,你是 xxx 吧,我们这里是 xxx 高端男士私人会所...”,握草,我先是一愣,然后狠狠的骂了回去。一脸傲娇的转过头,面带微笑稍显谄媚&#xff…

在Python中使用OpenCV裁剪图像

What is Cropping? 什么是播种? Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the…

面渣逆袭:RocketMQ二十三问

1.为什么要使用消息队列呢?消息队列主要有三大用途,我们拿一个电商系统的下单举例:解耦:引入消息队列之前,下单完成之后,需要订单服务去调用库存服务减库存,调用营销服务加营销数据……引入消息…

vue项目打包体积大优化之-productionSourceMap设置

一、productionSourceMap 的作用 productionSourceMap 在构建时生成完整的 SourceMap 文件,默认情况下开启。生产环境中启用 productionSourceMap 有助于开发者调试代码,可以在浏览器的调试工具中查看到源文件中错误的代码位置,而不是编译后…

Java日志性能那些事(转)

在任何系统中,日志都是非常重要的组成部分,它是反映系统运行情况的重要依据,也是排查问题时的必要线索。绝大多数人都认可日志的重要性,但是又有多少人仔细想过该怎么打日志,日志对性能的影响究竟有多大呢?…

如何在Java中使ArrayList只读?

使ArrayList只读 (Making ArrayList Read-Only) Given an ArrayList, and we have to make it Read-Only in Java. 给定一个ArrayList,我们必须使其成为Java只读。 Read-Only: If we make ArrayList as Read-Only i.e. we can only read ArrayList and we cannot p…

33岁程序员的年中总结

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)人生在不同的阶段会有不同的生活方式和思考问题的角度,这是一件非常有趣的事~ 比如,我在 22 岁会想&…

减治求有重复元素的全排列

求n个元素的全排列的所有解可以用减治法:每次拎出一个数做前缀,对剩下的元素再求全排列,直至只剩一个元素。代码源自《算法分析与设计(王晓东)》,复杂度O(n!) 1 //输出k~m的所有全排列2 void pe…

数据科学中的简单线性回归

简单线性回归 (Simple Linear Regression) A simple regression model could be a linear approximation of a causative relationship between two or additional variables. Regressions models are extremely valuable, as theyre one in every of the foremost common ways…

鹅厂一面,有关 ThreadLocal 的一切

1. 底层结构ThreadLocal 底层有一个默认容量为 16 的数组组成,k 是 ThreadLocal 对象的引用,v 是要放到 TheadLocal 的值public void set(T value) {Thread t Thread.currentThread();ThreadLocalMap map getMap(t);if (map ! null)map.set(this, valu…

再战“超融合”,戴尔、Nutanix绝世好CP

从进入PC领域开始,戴尔一直在扮演颠覆者的角色。戴尔的理想是以开放、标准化的技术和解决方案颠覆传统的封闭的技术和市场,实现与合作伙伴的共赢。在超融合架构逐渐兴起的今天,戴尔依旧希望以变革者的身份,携手超融合架构的先驱Nu…

ruby array_Ruby中带有示例的Array.index()方法

ruby arrayArray.index()方法 (Array.index() Method) In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of certain element. It is not as simple as it looks. Well, we w…