Rspec array equality
[1,2,3].should =~ [1,3,2]
is the same as
[1,2,3].sort.should == [1,3,2].sort
If you need to test whether two arrays contain same elements disregarding its order, you could use "=~" method.
Written by Dmitry Novotochinov
Related protips
4 Responses
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/5571/40899089b857df34ab04b17f01b00831.jpeg)
Superb! Just what i missed for so long.
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/4619/d3546cd8dd83b09f93976163101f971c.jpeg)
"If you need to test whether two arrays contain same elements disregarding its order"
In such a case you should you sets. ;)
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/2760/d66c760957d0411dc18c5b78fcb6c550.png)
@cypok yes, but only in case you have no duplicates.
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/8168/863e24c91fcf1cd3c879ea62a401365e.jpeg)
I prefer 'match_array' instead.
http://rubydoc.info/github/rspec/rspec-expectations/RSpec/Matchers:match_array
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Array
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#