diff --git a/lib/tzinfo/country.rb b/lib/tzinfo/country.rb index 4840d751..1bd216af 100644 --- a/lib/tzinfo/country.rb +++ b/lib/tzinfo/country.rb @@ -176,6 +176,16 @@ def hash code.hash end + # Matches `regexp` against the {code} of this {Country}. + # + # @param regexp [Regexp] a `Regexp` to match against the {code} of + # this {Country}. + # @return [Integer] the position the match starts, or `nil` if there is no + # match. + def =~(regexp) + regexp =~ code + end + # Returns a serialized representation of this {Country}. This method is # called when using `Marshal.dump` with an instance of {Country}. # diff --git a/test/tc_country.rb b/test/tc_country.rb index 4d9aeee5..1783e0bd 100644 --- a/test/tc_country.rb +++ b/test/tc_country.rb @@ -166,6 +166,17 @@ def test_hash assert_equal('US'.hash, Country.get('US').hash) end + define_method("test_=~_operator_matches_against_code") do + c = Country.get('GB') + assert_equal(0, c =~ /\AGB\z/) + assert_equal(1, c =~ /B/) + end + + define_method("test_=~_operator_returns_nil_when_regexp_does_not_match") do + c = Country.get('GB') + assert_nil(c =~ /US/) + end + def test_marshal c = Country.get('US') marshalled_c = Marshal.load(Marshal.dump(c))