Skip to content

Commit

Permalink
Add Country#=~ as an equivalent of Timezone#=~ (matching on code).
Browse files Browse the repository at this point in the history
  • Loading branch information
philr committed Apr 30, 2019
1 parent 8e4baff commit 8fdf06e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/tzinfo/country.rb
Expand Up @@ -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}.
#
Expand Down
11 changes: 11 additions & 0 deletions test/tc_country.rb
Expand Up @@ -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))
Expand Down

0 comments on commit 8fdf06e

Please sign in to comment.