Skip to content

Commit

Permalink
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
@@ -1001,6 +1001,12 @@ describe "String" do
str.sub(/(he|l|o)/, {"l" => "la"}).should be(str)
end

it "subs with regex and named tuple" do
str = "hello"
str.sub(/(he|l|o)/, {he: "ha", l: "la"}).should eq("hallo")
str.sub(/(he|l|o)/, {l: "la"}).should be(str)
end

it "subs using $~" do
"foo".sub(/(o)/) { "x#{$1}x" }.should eq("fxoxo")
end
2 changes: 1 addition & 1 deletion src/string.cr
Original file line number Diff line number Diff line change
@@ -1423,7 +1423,7 @@ class String
# "hello".sub(/(he|l|o)/, {"he": "ha", "l": "la"}) # => "hallo"
# "hello".sub(/(he|l|o)/, {"l": "la"}) # => "hello"
# ```
def sub(pattern : Regex, hash : Hash(String, _))
def sub(pattern : Regex, hash : Hash(String, _) | NamedTuple)
sub(pattern) { |match|
if hash.has_key?(match)
hash[match]

0 comments on commit 4fbab30

Please sign in to comment.