当前位置: 首页 > 工具软件 > JSON for Ruby > 使用案例 >

ruby 合并 json_在Ruby中合并两套

龙高超
2023-12-01

ruby 合并 json

This problem is all about finding a way to merge two sets. We have tried to find the solution with the help of two ways. Ruby is very rich in library. It has various predefined methods that are purposefully defined to fulfill some purpose. Here, for merging two sets, we can take help from the merge() method which is predefined in Ruby's library. Let us see the ways through which we are completing the task. We have two merge two sets or you can say that we have to store elements of two sets in the same or another set.

这个问题全是关于寻找一种方法来合并两个集合 。 我们尝试通过两种方法来找到解决方案。 Ruby的库非常丰富。 它具有各种预定义的方法,这些方法被有目的地定义为实现某些目的。 在这里,为了合并两个集合 ,我们可以从Ruby库中预定义的merge()方法获得帮助。 让我们看看完成任务的方式。 我们有两个合并两个集合,或者可以说我们必须将两个集合的元素存储在同一集合或另一个集合中。

Methods used:

使用的方法:

  • Set.new: This method is used to create a new instance of Set class.

    Set.new :此方法用于创建Set类的新实例。

  • Set.each: This method is used to fetch an individual element from the Set.

    Set.each :此方法用于从Set中获取单个元素。

  • Set.add(): Set.add method is predefined in Ruby's library which is used to add new elements in the Set.

    Set.add()Set.add方法是在Ruby的库中预定义的,用于在Set中添加新元素。

  • Set.merge(): This method is used to merge the set which is provided as the argument with the Set from which it is invoked.

    Set.merge() :此方法用于将作为参数提供的集合与调用它的Set合并。

Variables used:

使用的变量:

  • Vegetable: This is the first instance of Set class. It contains the name of vegetables.

    Vegetable :这是Set类的第一个实例。 它包含蔬菜的名称。

  • Fruits: This is the second instance of Set class. It contains the name of fruits.

    水果 :这是Set类的第二个实例。 它包含水果的名称。

  • i: This variable is used to specifying the serial of the Set. It is used inside the Set.each method.

    i :此变量用于指定Set的序列号。 它在Set.each方法内使用。

Example 1:

范例1:

=begin
Ruby program to merge two sets.
=end
require 'set'

Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])

Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

Vegetable.each do |element|
    Fruits.add(element)
end

i = 1
Fruits.each do |element|
	puts "#{i} => #{element}"
	i = i + 1 
end

Output

输出量

1 => Apple
2 => Mango
3 => Banana
4 => Orange
5 => Grapes
6 => potato
7 => brocolli
8 => broccoflower
9 => lentils
10 => peas
11 => fennel
12 => chilli
13 => cabbage

Explanation:

说明:

In the above code, we are using the Set.each method to merge two sets. We are using multiple methods for this purpose. We are also using the Set.add() method to add the element from one Set to another one. You can also observe the Set which is getting updated is the Fruit set.

在上面的代码中,我们使用Set.each方法合并两个set 。 为此,我们正在使用多种方法。 我们还使用Set.add()方法将元素从一个Set添加到另一个Set。 您还可以观察到正在更新的Set是Fruit set。

Example 2:

范例2:

=begin
Ruby program to merge two sets.
=end

require 'set'

Vegetable=Set.new(["potato","brocolli","broccoflower","lentils","peas","fennel","chilli","cabbage"])

Fruits = Set.new(["Apple","Mango","Banana","Orange","Grapes"])

Vegetable.merge(Fruits)

i = 1
Vegetable.each do |element|
	puts "#{i} => #{element}"
	i = i + 1 
end

Output

输出量

1 => potato
2 => brocolli
3 => broccoflower
4 => lentils
5 => peas
6 => fennel
7 => chilli
8 => cabbage
9 => Apple
10 => Mango
11 => Banana
12 => Orange
13 => Grapes

Explanation:

说明:

In the above code, we are taking help from Set.merge() method. The task gets pretty simple and can be done in a single line. The resultant set is stored in Vegetable set. You can also do Fruits.merge (Vegetables) if you want to store the result in the Fruit set. At last, we are printing the set with the help of Set.each method.

在上面的代码中,我们从Set.merge()方法获得帮助。 该任务变得非常简单,可以在一行中完成。 结果集存储在蔬菜集中。 如果要将结果存储在水果集中,也可以执行Fruits.merge (蔬菜)。 最后,我们将借助Set.each方法打印集合。

翻译自: https://www.includehelp.com/ruby/merge-two-sets.aspx

ruby 合并 json

 类似资料: