Skip to content
Permalink
Browse files

Add Country#=~ as an equivalent of Timezone#=~ (matching on code).

  • Loading branch information...
philr committed Apr 30, 2019
1 parent 8e4baff commit 8fdf06ed9a4b935864e19794187d091219de481a
Showing with 21 additions and 0 deletions.
  1. +10 −0 lib/tzinfo/country.rb
  2. +11 −0 test/tc_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}.
#
@@ -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))

0 comments on commit 8fdf06e

Please sign in to comment.
You can’t perform that action at this time.