ruby 集合 分组_从Ruby中的两个集合中找到共同的元素

ruby 集合 分组

Ruby is very rich in the library. It has various methods for meeting specific functionalities. In Ruby, operators are basically methods with two arguments generally. We have gone through the description of sets in Ruby. We have seen their implementation also. In sets, & operator works similarly to arithmetic and you can say that it takes two sets as arguments and finds the common elements or the repeated elements from both the sets. The return type of this method is a set and that set contains all the elements which are common in both the sets.

Ruby在库中非常丰富。 它具有满足特定功能的各种方法。 在Ruby中,运算符基本上是带有两个参数的方法。 我们已经对Ruby中的集合进行了介绍。 我们也看到了它们的实现。 在集合中, &运算符的工作方式与算术相似,可以说它以两个集合为参数,并从这两个集合中找到公共元素或重复元素。 此方法的返回类型是一个集合,并且该集合包含两个集合中共有的所有元素。

Methods used:

使用的方法:

  1. & : In ruby, most of the operators are considered as methods. This operator or method is used to find out the common elements from the sets provided as arguments to the method. The return type of this operator is set itself.

    :在ruby中,大多数运算符被视为方法。 此运算符或方法用于从作为方法参数提供的集合中找出公共元素。 此运算符的返回类型是自己设置的。

  2. set.each : set.each method is used to print the elements from the set one by one. It will provide you elements in the forward direction.

    set.eachset.each方法用于逐个打印集合中的元素。 它将为您提供前进方向的元素。

Variables used:

使用的变量:

  • Vegetable : It is a set. It is the first argument passed as the argument in & operator.

    蔬菜 :这是一组。 它是在&运算符中作为参数传递的第一个参数。

  • Sabzi : It is an instance of Set class. It is the second argument which is passed in & operator.

    Sabzi :它是Set类的实例。 这是在&运算符中传递的第二个参数。

  • New_set : It is containing the set which is returned from the & operator or method.

    New_set :它包含从&运算符或方法返回的集合。

Program:

程序:

=begin
Ruby program to show implementation of & operator
=end

require 'set'

Vegetable = Set.new(["potato", "tomato","brinjal","onion","peas","beetroot","chilli"])

Sabzi = Set.new(["potato", "tomato","brinjal","onion","beetroot","capsisum","chilli"])

New_set = Vegetable & Sabzi

New_set.each do |string|
	puts "#{string} element from new set"
end

Output

输出量

potato element from new set
tomato element from new set
brinjal element from new set
onion element from new set
beetroot element from new set
chilli element from new set

Explanation:

说明:

In the above code, it is shown how one can find the common elements from both sets? As you can see above, we have defined three sets, two sets are for carrying out the processing and one set is for storing the common elements from both the sets. We have taken help from the set.each method to print all the elements from the new set. As a result, you can find that the new set contains all the elements which are common in both the sets.

在上面的代码中,显示了如何从两个集合中找到共同的元素? 如上所示,我们定义了三个集合,两个集合用于执行处理,一个集合用于存储两个集合中的公共元素。 我们从set.each方法获得帮助,以打印新集中的所有元素。 结果,您会发现新集合包含两个集合中共有的所有元素。

翻译自: https://www.includehelp.com/ruby/find-the-common-elements-from-two-sets-in-ruby.aspx

ruby 集合 分组

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值