ruby 集合 分组_在Ruby中打印集合的元素

ruby 集合 分组

We have gone through the implementation of sets in Ruby. They are very similar to arrays. Now, let us see how we print the elements present in a set. There are no indexes present in the sets because sets are used to store the large elements.

我们已经完成了Ruby中集合的实现。 它们与数组非常相似。 现在,让我们看看如何打印一组中存在的元素。 集合中没有索引,因为集合用于存储大元素。

Methods used:

使用的方法:

  • set.add(): This method is used to add the elements in the set. It would only add the elements which are not present in the set. You will not get an error if you try to add duplicate elements but duplicate elements will not be reflected in the set.

    set.add() :此方法用于添加集合中的元素。 它只会添加集合中不存在的元素。 如果您尝试添加重复的元素,但不会出现错误,但是重复的元素将不会反映在集合中。

  • set.each: This method is used to print the elements in the set. Whereas you cannot make changes in the set or in other words you cannot manipulate the set elements with the help of this method. We have got several set methods to manipulate the elements. This method will only print the elements in the forward direction.

    set.each :此方法用于打印集合中的元素。 然而,您无法在集合中进行更改,或者换句话说,您无法借助此方法来操作集合元素。 我们有几种设置方法来操作元素。 此方法将仅向前打印元素。

  • set.size(): This method tells the size of the set or the number of elements present in the set.

    set.size() :此方法告知集合的大小或集合中存在的元素数。

Variables used:

使用的变量:

  • vegetable: This is a set that contains the names of vegetables.

    蔬菜 :这是一个包含蔬菜名称的集合。

  • i: This is working as a tracking variable which is telling the order of elements present in the set.

    i :这是一个跟踪变量,用于指示集合中元素的顺序。

Code:

码:

=begin
Ruby program to implement set.each method 
=end

require 'set'

# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

Vegetable.add("potato")

Vegetable.add("tomato")

Vegetable.add("Beetroot")

puts "Number of elements in set are #{Vegetable.size()}"

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

Output

输出量

Number of elements in set are 5
1 element is potato
2 element is tomato
3 element is brinjal
4 element is onion
5 element is Beetroot

Explanation:

说明:

In the above code, first, we have created a set named Vegetable. We have made use of set.add() method to add more elements in the Vegetable set. Our objective is to print the elements present in the set. We can only do this with the help of the set.each method. It can also be considered as a loop that works on specific variables in Ruby. The variable ‘n’ is receiving the elements present in the set one by one. We have employed a variable i to print the order of the elements.

在上面的代码中,首先,我们创建了一个名为Vegetable的集合。 我们利用set.add()方法在Vegetable集中添加了更多元素。 我们的目标是打印集中存在的元素 。 我们只能在set.each方法的帮助下执行此操作 。 也可以将其视为对Ruby中特定变量起作用的循环。 变量“ n”正在一个接一个地接收集合中存在的元素。 我们使用了变量i来打印元素的顺序。

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

ruby 集合 分组

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值