新手问题 Ruby 数组的 减法是通过什么判断两个对象是否相等的?

liuvz · 2016年10月23日 · 最后由 gihnius 回复于 2016年10月24日 · 1774 次阅读

新手求教!!!! 如题:ruby 数组的 减法是通过什么判断两个对象是否相等的?自己的类想支持数组减法应该实现哪些方法?

puts "诶呦,这个发帖好高科技啊!!!!"

我以为他会执行代码呢·····

It compares elements using their hash and eql? methods for efficiency.

http://ruby-doc.org/core-2.3.1/Array.html#method-i-2D

对象其实就是一个内存地址吧,内存地址相等就是相等了。

[84] pry(main)> show-doc Array#-

From: array.c (C Method):
Owner: Array
Visibility: public
Signature: -(arg1)
Number of lines: 11

Array Difference

Returns a new array that is a copy of the original array, removing any
items that also appear in other_ary. The order is preserved from the
original array.

It compares elements using their #hash and #eql? methods for efficiency.

   [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ]  #=>  [ 3, 3, 5 ]

If you need set-like behavior, see the library class Set.
liuvz 关闭了讨论。 12月20日 22:45
需要 登录 后方可回复, 如果你还没有账号请 注册新账号