ruby 集合 分组_在Ruby中找到集合的长度

ruby 集合 分组

We know the way to declare the instance of the set class in Ruby. Most of the time, we may need to find the length of the set or you can say that we may need to find the number of elements in the specific set. Ruby is very rich in library and it provides you a method known as Set.length() method which returns the number of elements present in the Set. But, we should know how to solve things without method also? We will focus on two ways through which we can find the length of the Set. The first one is without using method and the second one is with the help of the method.

我们知道在Ruby中声明set类实例的方法。 大多数时候,我们可能需要找到集合的长度,或者可以说我们可能需要找到特定集合中元素的数量 。 Ruby库非常丰富,它为您提供了一种称为Set.length()方法的方法 ,该方法返回Set中存在的元素数 。 但是,我们也应该知道如何不用方法解决问题吗? 我们将集中于两种方法来找到Set的长度 。 第一个没有使用方法,第二个没有使用方法。

Methods used:

使用的方法:

  • Set.each: It is used to traverse the set. It processes the elements of the set one by one.

    Set.each :用于遍历集合。 它逐一处理集合的元素。

  • Set.length: This method is a predefined method in Ruby’s library which is purposefully defined to find the number of elements present in the set. The return type of this method is an integer which is equivalent to the length of the set from which it has been invoked.

    Set.length :此方法是Ruby库中的预定义方法,该方法有意定义为查找集合中存在的元素数。 此方法的返回类型是一个整数,它等于从中调用该方法的集合的长度。

  • Set.merge: This method merges two sets and stores the resultant set in that set from which it has been invoked. The return type of this method is Set.

    Set.merge :此方法合并两个集合并将结果集存储在从中调用它的那个集合中。 该方法的返回类型为Set。

Variables used:

使用的变量:

  • Vegetable: This is an instance of Set class.

    Vegetable :这是Set类的实例。

  • Fruits: This is an instance of Set class.

    水果 :这是Set类的实例。

  • Count: It is acting as a counter variable. It is counting the number of elements present in the Set.

    计数 :它用作计数器变量。 它正在计算集合中存在的元素数。

Example 1:

范例1:

=begin
Ruby program to find the length of the set.
=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)

count = 0
Vegetable.each do |element|
	count = count + 1 
end

puts "The number of elements present in the set are #{count}"

Output

输出量

The number of elements present in the set are 13

Explanation:

说明:

In the above code, we are trying to find the length of the resultant set. First, we have declared two sets and then carrying out the merging process. We are passing the elements of a resultant set into Set.each loop. Inside Set.each loop, we are incrementing the count variable by 1. count variable is initialized with 0. At last, we are getting the number of elements present in the set.

在上面的代码中,我们试图找到结果集的长度 。 首先,我们声明了两个集合,然后执行合并过程。 我们将结果集的元素传递到Set.each循环中。 在Set.each循环内部,我们将count变量加1。count变量用0初始化。最后,我们获取set中存在的元素数。

Example 2:

范例2:

=begin
Ruby program to find the length of the set.
=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)

puts "The number of elements present in the set are #{Vegetable.length}"

Output

输出量

The number of elements present in the set are 13

Explanation:

说明:

In the above code, we have declared two instances or objects of Set class. We are first merging them and storing the result in the Vegetable set. We are then finding the length of the resultant set with the help of Set.length method which is a predefined method in Ruby.

在上面的代码中,我们声明了Set类的两个实例或对象。 我们首先将它们合并,并将结果存储在蔬菜集中。 然后,我们使用Set.length方法(这是Ruby中的预定义方法)来找到结果集的长度

翻译自: https://www.includehelp.com/ruby/find-the-length-of-a-set.aspx

ruby 集合 分组

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值