_.uniq_Ruby中带有示例的Array.uniq方法

_.uniq

Array.uniq方法 (Array.uniq Method)

In this article, we will study about Array.uniq Method. You all must be thinking the method must be doing something related to finding unique elements or objects from the Array instance. It is not as simple as it looks. Well, we will figure this out in the rest of our content. We will try to understand it with the help of syntax and demonstrating program codes.

在本文中,我们将研究Array.uniq方法 。 你们都必须认为该方法必须做一些与从Array实例中查找唯一元素或对象有关的事情。 它并不像看起来那么简单。 好吧,我们将在其余内容中解决这个问题。 我们将尝试借助语法并演示程序代码来理解它。

Method description:

方法说明:

This method is a public instance method and defined for the Array class in Ruby’s library. This method works in a way that it returns a new array that contains all the unique elements from the self-Array instance. If you are providing a block along with the method at the time of its invocation then it will use the return value of block for the comparison. This method will not create an impact on the actual arrangement of objects in the self-Array because this method belongs to the category of non-destructive methods where the changes created by the method are temporary or non-permanent.

该方法是一个公共实例方法,为Ruby库中的Array类定义。 此方法的工作方式是返回一个新数组,该数组包含self-Array实例中的所有唯一元素。 如果在调用时随方法一起提供了一个块,则它将使用该块的返回值进行比较。 此方法不会对自数组中对象的实际排列产生影响,因为此方法属于非破坏性方法的类别,在该类别中,该方法创建的更改是临时的或非永久的。

Syntax:

句法:

    array_instance.uniq -> Array or nil
    or
    array_instance.uniq{|item ...| block}-> new_array

Argument(s) required:

所需参数:

This method does not take any argument. Instead, you will have to pass a block if you want to find unique objects based on certain criteria.

此方法不带任何参数。 相反,如果您要根据特定条件查找唯一对象,则必须传递一个块。

Example 1:

范例1:

=begin
  Ruby program to demonstrate uniq method
=end

# array declaration
table = ["Subha","Sham","Sham","Vivek","Me","Amisha","Zain","Subha","Vivek"]

puts "Array uniq implementation"
new_arr = table.uniq

puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"

Output

输出量

Array uniq implementation
Array after finding unique: ["Subha", "Sham", "Vivek", "Me", "Amisha", "Zain"]
Original Array instance: ["Subha", "Sham", "Sham", "Vivek", "Me", "Amisha", "Zain", "Subha", "Vivek"]

Explanation:

说明:

In the above code, you can observe that we are finding unique elements in the Array instance with the help of the Array.uniq method. In the new Array, we have all the objects which are unique in the self Array. This method is not creating an impact upon the original Array instance because this method is one of the examples of non-destructive methods.

在上面的代码中,您可以观察到借助于Array.uniq方法,我们正在Array实例中找到唯一元素。 在新的数组中,我们拥有自数组中唯一的所有对象。 此方法不会对原始Array实例产生影响,因为该方法是非破坏性方法的示例之一。

Example 2:

范例2:

=begin
  Ruby program to demonstrate uniq method
=end

# array declaration
table = ["Subha","Sham","Sham","Vivek","Me","Amisha","Zain","Subha","Vivek"]

puts "Array uniq implementation"
new_arr = table.uniq

puts "Array after finding unique: #{new_arr}"
puts "Original Array instance: #{table}"

Output

输出量

Array uniq implementation
Array after finding unique: [["Subha", "Sham"], ["din", "dopher"]]
Original Array instance: [["Subha", "Sham"], ["Subha", "Raat"], ["din", "dopher"]]

Explanation:

说明:

In the above output, you can notice that we are calling the Array.uniq method with a block. Our self Array is a multidimensional Array that contains three sub Arrays. Though all the three Arrays are unique the method had carried out the comparison of the basis of the first object of all the three Array instances. This method is not creating an impact upon the original Array instance because this method is one of the examples of non-destructive methods.

在上面的输出中,您可以注意到我们正在使用一个块调用Array.uniq方法 。 我们的self Array是一个多维数组,其中包含三个子数组。 尽管所有三个数组都是唯一的,但该方法已对所有三个数组实例的第一个对象的基础进行了比较。 此方法不会对原始Array实例产生影响,因为该方法是非破坏性方法的示例之一。

翻译自: https://www.includehelp.com/ruby/array-uniq-method-with-example.aspx

_.uniq

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值