Permalink
Browse files

Implement equality operator as per suggestion by haxney

  • Loading branch information...
1 parent b56b80c commit 231f2489fad0a6c3b3b222d76db7ef7e1722fa0a @carr committed Jun 23, 2011
Showing with 17 additions and 0 deletions.
  1. +6 −0 lib/phone.rb
  2. +11 −0 test/phone_test.rb
View
@@ -235,6 +235,12 @@ def has_default_country_code?
def has_default_area_code?
area_code == self.class.default_area_code
end
+
+ # comparison of 2 phone objects
+ def ==(other)
+ methods = [:country_code, :area_code, :number, :extension]
+ methods.all? { |method| other.respond_to?(method) && send(method) == other.send(method) }
+ end
private
View
@@ -103,4 +103,15 @@ def test_doesnt_validate
assert_equal Phoner::Phone.valid?('385915125486'), false
end
+ def test_comparison_true
+ pn1 = Phoner::Phone.new '5125486', '91', '385'
+ pn2 = Phoner::Phone.new '5125486', '91', '385'
+ assert pn1 == pn2
+ end
+
+ def test_comparison_false
+ pn1 = Phoner::Phone.new '5125486', '91', '385'
+ pn2 = Phoner::Phone.new '1234567', '91', '385'
+ assert pn1 != pn2
+ end
end

0 comments on commit 231f248

Please sign in to comment.