Skip to content

Commit 5fb0438

Browse files
makenowjustasterite
authored andcommittedJun 12, 2017
MatchData: raise KeyError instead of ArgumentError
Currently #[](key: String) raises `ArgumentError`. I think it should raise `KeyError` because it looks like `Hash` operation.
1 parent 61f21ae commit 5fb0438

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed
 

‎spec/std/match_data_spec.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ describe "Regex::MatchData" do
3434

3535
it "raises exception when named group doesn't exist" do
3636
("foo" =~ /foo/).should eq(0)
37-
expect_raises(ArgumentError) { $~["group"] }
37+
expect_raises(KeyError) { $~["group"] }
3838
end
3939

4040
it "raises if outside match range with []" do

‎src/regex/match_data.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ class Regex
148148
end
149149

150150
# Returns the match of the capture group named by *group_name*, or
151-
# raises an `ArgumentError` if there is no such named capture group.
151+
# raises an `KeyError` if there is no such named capture group.
152152
#
153153
# ```
154154
# "Crystal".match(/r(?<ok>ys)/).not_nil!["ok"] # => "ys"
155-
# "Crystal".match(/r(?<ok>ys)/).not_nil!["ng"] # raises ArgumentError
155+
# "Crystal".match(/r(?<ok>ys)/).not_nil!["ng"] # raises KeyError
156156
# ```
157157
def [](group_name : String)
158158
match = self[group_name]?
159159
unless match
160-
raise ArgumentError.new("Match group named '#{group_name}' does not exist")
160+
raise KeyError.new("Match group named '#{group_name}' does not exist")
161161
end
162162
match
163163
end

0 commit comments

Comments
 (0)
Please sign in to comment.