ruby 集合 分组
In this program, we will see how we can combine the two sets? This is not a very difficult task. This can be easily done with the help of the + operator. In many places of programming, you will find that + operator is overloaded for various types to meet various purposes. Here in the case of sets, + operator works likes arithmetic OR. It combines all the elements from both the sets and returns a set that contains all those elements. Let us look at the code and understand how we can complete this task.
在此程序中,我们将看到如何将两个集合结合起来 ? 这不是一个非常困难的任务。 可以在+运算符的帮助下轻松完成此操作 。 在许多编程地方,您会发现+运算符对于各种类型都已重载以满足各种目的。 在集合的情况下, +运算符的作用类似于算术OR 。 它组合了两个集合中的所有元素,并返回包含所有这些元素的集合。 让我们看一下代码,了解如何完成此任务。
Methods used:
使用的方法:
+ : In ruby, most of the operators are considered as methods. This operator or method is used to combine two sets provided as arguments to the method. The return type of this operator is set itself. The set returned is having all the elements which are present in both the sets. Syntax:
+ :在Ruby中,大多数运算符被视为方法。 此运算符或方法用于合并作为方法的参数提供的两个集合。 此运算符的返回类型是自己设置的。 返回的集合具有两个集合中都存在的所有元素。 句法:
SetA + SetB
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.each:-set.each方法用于逐个打印集合中的元素。 它将为您提供前进方向的元素 。
Variables used:
使用的变量:
Vegetable : It is an instance of Set class. It is the first argument passed as the argument in + operator.
Vegetable :它是Set类的一个实例。 它是在+运算符中作为参数传递的第一个参数。
Sabzi : It is an instance of Set class. It is the second argument that 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","cabbage"])
Sabzi=Set.new(["potato","tomato","brinjal","onion","beetroot","capsicum","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
peas element from new set
beetroot element from new set
chilli element from new set
cabbage element from new set
capsicum element from new set
Explanation:
说明:
In the above code, it is shown how one can combine all the elements from both the 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 observe that there is no repetition of elements. It is having all the elements and each element is present only for one time. Elements are appearing single even if they are present in both sets.
在上面的代码中,显示了如何组合两个集合中的所有元素? 如上所示,我们定义了三个集合,两个集合用于执行处理,一个集合用于存储两个集合中的公共元素。 我们从set.each方法获得帮助,以打印新集中的所有元素。 结果,您可以观察到元素没有重复。 它具有所有元素,每个元素仅出现一次。 即使元素出现在两个集合中,它们也都显示为单个。
翻译自: https://www.includehelp.com/ruby/join-all-the-elements-of-two-sets-in-ruby.aspx
ruby 集合 分组