ruby 数组查找出现次数_在Ruby中查找两个数组是否相同

ruby 数组查找出现次数

In the last article, we have seen how we can implement <=> operator to find the difference between two Array instances?

在上一篇文章中,我们看到了如何实现<=>运算符来查找两个Array实例之间的区别?

The method was giving different results for different scenarios like -1 if the first Array instance is less than the second Array instance. We can verify two Array instances are identical or not with the help of <=> also as when this operator throws 0 as the result, it means that two Array instances are the same. In the article, we will see two more ways through which we can find alternative solutions to the situation and the situation is, as mentioned Finding two instances of Array class are same or not.

如果第一个Array实例小于第二个Array实例,则对于-1等不同情况,该方法将给出不同的结果。 我们可以借助<=>来验证两个Array实例是否相同,也可以使用此运算符将结果抛出0来表示这两个Array实例相同。 在本文中,我们将看到另外两种方式,可以通过这种方式找到该情况的替代解决方案,并且如前所述, 找到Array类的两个实例是否相同

When two objects of Array class are the same, this means that they both are of the same length and contain the same element values. This can be checked with the help of == operator as well as with the help of .eql? method. Well, they both are Public instance method living inside Ruby library. Both the ways will give you a response in Boolean value, they will give true if the Arrays are equal and false when the Array instances are not identical. Let us see their syntaxes and examples for understanding them in a better manner.

当Array类的两个对象相同时,这意味着它们都具有相同的长度并且包含相同的元素值。 可以使用==运算符以及.eql?进行检查。 方法。 好吧,它们都是Ruby库中的Public实例方法。 两种方式都会给您一个布尔值的响应,如果Arrays相等,它们将给出true;如果Array实例不相同,它们将得到false。 让我们看看它们的语法和示例,以更好地理解它们。

Method 1: Checking with the help of == method.

方法1:借助==方法进行检查。

Syntax:

句法:

    array1 == array2

Example:

例:

=begin
  Ruby program to demonstrate == operator
=end

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]

# comparing
rslt = array1 == array2

# printing the result
if rslt == true
	puts "Both the Array instances are identical"
else
	puts "Array instances are not identical"
end

Output

输出量

Both the Array instances are identical

Explanation:

说明:

In the above code, you can observe that we have created two Array instance. We have checked their equality with the help of == operator. We have stored the Boolean result inside a variable and as a result, we got that both the Array instances are identical as the method has returned "true".

在上面的代码中,您可以观察到我们已经创建了两个Array实例。 我们在==运算符的帮助下检查了它们的相等性。 我们将布尔结果存储在一个变量中,结果,两个Array实例都相同,因为该方法返回了“ true”

Method 2: Checking with the help of .eql? method.

方法2:在.eql的帮助下进行检查? 方法。

Syntax:

句法:

    array1.eql?(array2)

Example:

例:

=begin
  Ruby program to demonstrate .eql? method
=end

# arrays
array1 = [1,"Ramesh","Apple",12,true,nil,"Satyam","Harish"]
array2 = [1,"Ramesh","Apple",12,true,0,"Satyam","Harish"]

# comparing
rslt = array1.eql?(array2)

# printing the result
if rslt == true
	puts "Both the Array instances are identical"
else
	puts "Array instances are not identical"
end

Output

输出量

Array instances are not identical

Explanation:

说明:

In the above code, you can observe that we have created two Array instance. We have checked their equality with the help of .eql? method. We have stored the Boolean result inside a variable and as a result, we got that both the Array instances are not identical as the method has returned "false".

在上面的代码中,您可以观察到我们已经创建了两个Array实例。 我们已经在.eql的帮助下检查了它们的相等性? 方法 。 我们将布尔结果存储在一个变量中,结果,两个Array实例都不相同,因为该方法返回了“ false”

翻译自: https://www.includehelp.com/ruby/finding-whether-two-arrays-are-identical-or-not.aspx

ruby 数组查找出现次数

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值