Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 比较两个数组的内容相等性_Ruby - Fatal编程技术网

Ruby 比较两个数组的内容相等性

Ruby 比较两个数组的内容相等性,ruby,Ruby,我想比较两个数组,但assert_equal引发错误: No visible difference in the Array#inspect output. You should look at the implementation of #== on Array or its members. 这是我的测试: describe Post do before do @draft = Post.new('test-draft', draft = true)

我想比较两个数组,但
assert_equal
引发错误:

No visible difference in the Array#inspect output.
You should look at the implementation of #== on Array or its members.
这是我的测试:

describe Post do
    before do
        @draft = Post.new('test-draft', draft = true)
        @post = Post.new('2014-10-31-test-post')
    end

    describe '.drafts' do
        it 'returns an array of unpublished posts' do
            actual = Post.drafts           
            assert_equal [@draft], actual
        end
    end
end
这是我的两个数组,它们的内容相同,但内存位置不同

[
    [0] #<Post:0x0000000851a178 @file_name="test-draft", @draft=true>
]
[
    [0] #<Post:0x0000000851a538 @file_name="test-draft", @draft=true>
]
[
[0] #
]
[
[0] #
]

如何比较我的两个数组?

既然您想查看值是否正确,您可以实现一个类似于helper的方法,该方法将实现以下内容

a = [1,2,3]
b = [1,2,3]

((a - b) + (b - a)).empty?
#=> true
乙二醇


因为您想知道这些值是否正确,所以可以实现一个类似于helper的方法

a = [1,2,3]
b = [1,2,3]

((a - b) + (b - a)).empty?
#=> true
乙二醇


听起来你的Post类没有自己的
=
操作符,所以没有两个Post对象是相等的。我想你应该给Post一些相等的概念。

听起来你的Post类没有自己的
=
操作符,所以没有两个Post对象是相等的。我想你会想给波斯特一些平等的概念