自定義對象我有一個類大致是這樣的:Set.include?使用Ruby
class C
attr_accessor :board # board is a multidimensional array (represents a matrix)
def initialize
@board = ... # initialize board
end
def ==(other)
@board == other.board
end
end
然而,當我這樣做:
s = Set.new
s.add(C.new)
s.include?(C.new) # => false
爲什麼?
你試過了嗎?我做了,沒有工作。 – steenslag
好點 - 我認爲我們也需要'哈希'。更正... –
我upvoted,但接受steenslag的答案,因爲它顯示'eql?'不是由Hash調用,這對我很有趣。不管怎樣,謝謝你。 – whatyouhide