Skip to content

Instantly share code, notes, and snippets.

@vanakenm
Created February 21, 2013 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vanakenm/5005500 to your computer and use it in GitHub Desktop.
Save vanakenm/5005500 to your computer and use it in GitHub Desktop.
Can someone explain to me how the "-" method work on array ?
require 'minitest/autorun'
require 'minitest/spec'
class ElementTest
attr_accessor :value
def ==(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 true #success
arr2.include?(et1).must_equal false #success
arr1.include?(et2).must_equal true #success
arr1.include?(et22).must_equal true #success
arr1.include?(et3).must_equal false #success
arr2.select{|v|!arr1.include?(v)}.size.must_equal 1 #success
arr1.select{|v|!arr2.include?(v)}.size.must_equal 1 #success
(arr2-arr1).size.must_equal 1 #fail
(arr1-arr2).size.must_equal 1 #fail
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment