-
-
Save toch/5011462 to your computer and use it in GitHub Desktop.
Because - use eql? operator and include use == operator, you should take care of what you can obtain with a - (that use key hash to make the search) and include (that use object comparator to test)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'minitest/autorun' | |
require 'minitest/spec' | |
class ElementTest | |
attr_accessor :value | |
def eql?(other) | |
value == other.value | |
end | |
def hash | |
value.hash | |
end | |
end | |
describe "Test" do | |
it "should be equal" do | |
et1 = ElementTest.new | |
et1.value = "1" | |
et2 = ElementTest.new | |
et2.value = "2" | |
et22 = ElementTest.new | |
et22.value = "2" | |
et3 = ElementTest.new | |
et3.value = "3" | |
arr1 = [et1,et22] | |
arr2 = [et2,et3] | |
arr2.include?(et2).must_equal true #success | |
arr2.include?(et22).must_equal false #success | |
arr2.include?(et1).must_equal false #success | |
arr1.include?(et2).must_equal false #success | |
arr1.include?(et22).must_equal true #success | |
arr1.include?(et3).must_equal false #success | |
arr2.select{|v|!arr1.include?(v)}.size.must_equal 2 #success | |
arr1.select{|v|!arr2.include?(v)}.size.must_equal 2 #success | |
(arr2-arr1).size.must_equal 1 #success | |
(arr1-arr2).size.must_equal 1 #success | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment