您的位置:首页 > 编程语言 > Ruby

ruby_Ruby套装

2020-08-03 03:16 1721 查看

ruby

设置为Ruby (Set in Ruby)

In Ruby, the set is a class that is very similar to an array but it has some special attributes which make the processing faster. You must have observed that you can insert duplicate elements in an array but this is not the case of the set. The set does not allow the insertion of non-unique elements implicitly. So, in the future, if you will add elements after the definition of a set, you need not worry about the duplicity. It would not allow the accommodation of repeated elements. Following is the way to declare and define the Ruby set.

在Ruby中, 集合是一个与数组非常相似的类,但是它具有一些特殊的属性,这些属性使处理速度更快。 您必须已经观察到可以在数组中插入重复的元素,但这不是set的情况。 该集合不允许隐式插入非唯一元素。 因此,将来,如果您将在集合的定义之后添加元素,则不必担心重复性。 这将不允许重复元素的容纳。 以下是声明和定义Ruby集的方法

set_name = Set.new([element1,element2,...])
[/code]

Now, let us see how we declare, define and implement a set with the help of some supporting examples?

现在,让我们看看如何借助一些支持示例来声明,定义和实现一个集合?

Example 1:

范例1:

=begin
Ruby program to demonstrate set
=end

require 'set'

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

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

Output

输出量

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
[/code]

In the above example, you can observe that we are creating a set named Vegetable as per the syntax given above. You can also observe that we need to put the statement require 'set' before starting to write code. We are only calculating the number of elements present in the set with the help of the set.size() method. Writing the set name along with the puts statement simply gives us the information about the set elements.

在上面的示例中,您可以观察到根据上面给出的语法,我们正在创建一个名为Vegetable的集合。 您还可以观察到,在开始编写代码之前,我们需要将语句要求为“ set” 。 我们仅在set.size()方法的帮助下计算集合中存在的元素数。 将集合名称与puts语句一起写就可以为我们提供有关集合元素的信息。

We have seen how to add elements in the set at the time of the declaration? Now, let us see how to add elements afterward?

我们已经看到了如何在声明时在集合中添加元素? 现在,让我们看看之后如何添加元素?

Example 2:

范例2:

=begin
Ruby program to demonstrate set
=end

require 'set'

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

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

puts Vegetable

Vegetable.add("Cauliflower")

Vegetable.add("Peas")

Vegetable.add("Beetroot")

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

puts Vegetable
[/code]

Output

输出量

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>Number of elements in set are 7
#<Set: {"potato", "tomato", "brinjal", "onion", "Cauliflower", "Peas", "Beetroot"}>
[/code]

In the code given above, you can observe that we are adding elements in the set Vegetable afterward with the help of set.add("Element") method. Then we are again printing the set size and the elements.

在上面给出的代码中,您可以观察到,之后我们借助set.add(“ Element”)方法在set Vegetable中添加了元素。 然后,我们再次打印设置的大小和元素。

Example 3:

范例3:

=begin
Ruby program to demonstrate set
=end

require 'set'

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

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

puts Vegetable

Vegetable.add("potato")

Vegetable.add("tomato")

Vegetable.add("Beetroot")

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

puts Vegetable
[/code]

Output

输出量

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>Number of elements in set are 5
#<Set: {"potato", "tomato", "brinjal", "onion", "Beetroot"}>
[/code]

In the above code, after observing the output you must be thinking that the size of the set is 5 even after adding 3 more elements. This is because the elements which we were trying to add are already present in the set. It doesn’t allow the repetition of elements. You will not get an error but the duplicate elements will be discarded.

在上面的代码中,观察输出后,您必须考虑到即使再添加3个元素,集合的大小也是5。 这是因为我们试图添加的元素已经存在于集合中。 它不允许重复元素。 您不会收到错误,但是重复的元素将被丢弃。

翻译自: https://www.includehelp.com/ruby/set.aspx

ruby

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: