ruby中实例变量与属性_在Ruby中将数组实例与<=>进行比较

ruby中实例变量与属性

In the last article, we have seen how one can add an object into an Array element with the help of << operator? That was a Public instance method. In this article, we will see how we can compare two Array instances with the help of <=> operator? This operator is also one of the examples of Public instance methods. Let us see how we can carry out this comparison.

在上一篇文章中,我们看到了如何借助<<操作符将对象添加到Array元素中? 那是一个公共实例方法。 在本文中,我们将看到如何在<=>运算符的帮助下比较两个Array实例? 此运算符也是Public实例方法的示例之一。 让我们看看如何进行比较。

Method description

方法说明

In the case of this method, the comparison is carried out in an element-wise manner. Sometimes, the result is dependent upon the comparison of the first elements of corresponding Array instances only. This method works in a way that each object of each Array is compared with the help of <=> operator. The first two elements that are not identical to each other determine the result of the whole comparison. Now, we will be studying cases that will determine the result of the comparison being carried out between two instances of Array class.

在这种方法的情况下,以元素方式进行比较。 有时,结果仅取决于相应Array实例的前几个元素的比较。 此方法的工作方式是在<=>运算符的帮助下比较每个Array的每个对象。 彼此不相同的前两个元素确定了整个比较的结果。 现在,我们将研究一些案例,这些案例将确定在Array类的两个实例之间进行比较的结果。

Case 1: When Array is lesser than other Array.

情况1:当Array小于其他Array时。

The return type of this method is an integer and it will return -1 when the first Array instance is lesser than another Array instance. Let us understand this with the help of an example and observe the result of the comparison.

此方法的返回类型是整数,当第一个Array实例小于另一个Array实例时,它将返回-1。 让我们借助示例来理解这一点,并观察比较的结果。

Example:

例:

=begin
  Ruby program to demonstrate <=>
=end

# arrays
first_array = ["a","a","c"]
second_array = ["a","b","c"]

# comparing
rslt = first_array <=> second_array

# printing the result
puts "The result of comparison is #{rslt}"

Output

输出量

The result of comparison is -1

Explanation:

说明:

You can observe that the result of the comparison is -1 because, in both the arrays, differences are coming at the second index. The object "a" is smaller than the object "b", that is why we are getting -1 as the result of the comparison.

您可以观察到比较的结果是-1,因为在两个数组中,差异都在第二个索引处。 对象“ a”小于对象“ b” ,这就是为什么比较结果为-1的原因。

Case 2: When Array is greater than other Array.

情况2:当Array大于其他Array时。

The return type of this method is an integer and it will return 1 when the first Array instance is greater than another Array instance. Let us understand this with the help of an example and observe the result of the comparison.

此方法的返回类型是整数,当第一个Array实例大于另一个Array实例时,它将返回1。 让我们借助示例来理解这一点,并观察比较的结果。

Example:

例:

=begin
  Ruby program to demonstrate <=>
=end

# arrays
first_array = ["a","c","c"]
second_array = ["a","a","c"]

# comparing
rslt = first_array <=> second_array

# printing the result
puts "The result of comparison is #{rslt}"

Output

输出量

The result of comparison is 1

Explanation:

说明:

You can observe that the result of the comparison is 1 because, in both the arrays, differences are coming at the second index. The object "c" is greater than the object "a", that is why we are getting 1 as the result of the comparison.

您可以观察到比较的结果为1,因为在两个数组中,差异都在第二个索引处。 对象“ c”大于对象“ a” ,这就是为什么比较结果为1的原因。

Case 3: When both the Array instances are equal.

情况3:当两个Array实例相等时。

When both the array instances are equal then this method will return 0. Two Array instances are called to be equal if and only if the values of both the Arrays are the same and the length of the first Array is equal to the length of another Array. Let us understand this with the help of an example and observe the result of the comparison.

当两个数组实例相等时,此方法将返回0 。 当且仅当两个数组的值相同并且第一个数组的长度等于另一个数组的长度时,两个数组实例才被称为相等。 让我们借助示例来理解这一点,并观察比较的结果。

Example:

例:

=begin
  Ruby program to demonstrate <=>
=end

# arrays 
first_array = ["a","c","c"]
second_array = ["a","c","c"]

# comparing
rslt = first_array <=> second_array

# printing the result
puts "The result of comparison is #{rslt}"

Output

输出量

The result of comparison is 0

Explanation:

说明:

In the above code, you can observe that both the Arrays are exactly the same in length as well as in values that is why we are getting 0 as the result of the comparison.

在上面的代码中,您可以观察到两个数组的长度和值都完全相同,这就是为什么比较结果为0的原因。

Case 4: When two values are incomparable.

情况4:两个值不可比。

You will get 'nil' as the result of comparison when two values are incomparable to each other. Let us understand this with the help of an example and observe the output.

当两个值彼此不可比时,您将得到“ nil”作为比较的结果。 让我们借助示例来理解这一点并观察输出。

Example:

例:

=begin
  Ruby program to demonstrate <=>
=end

# arrays
first_array = ["a","c","c"]
second_array = [1,2,3]

# comparing
rslt = first_array <=> second_array

# printing the result
puts "The result of comparison is #{rslt}"

Output

输出量

The result of comparison is 

Explanation:

说明:

In the above code, you can observe that we are getting 'nil' as the result of comparison because integer value can't be compared with character value.

在上面的代码中,您可以看到比较的结果是“ nil” ,因为不能将整数值与字符值进行比较。

翻译自: https://www.includehelp.com/ruby/comparing-array-instances.aspx

ruby中实例变量与属性

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值