Skip to content

Commit 4fbab30

Browse files
maihaAry Borenszweig
authored and
Ary Borenszweig
committedJan 4, 2017
Add String#sub(Regex, NamedTuple)
1 parent d4ef40c commit 4fbab30

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed
 

Diff for: ‎spec/std/string_spec.cr

+6
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,12 @@ describe "String" do
10011001
str.sub(/(he|l|o)/, {"l" => "la"}).should be(str)
10021002
end
10031003

1004+
it "subs with regex and named tuple" do
1005+
str = "hello"
1006+
str.sub(/(he|l|o)/, {he: "ha", l: "la"}).should eq("hallo")
1007+
str.sub(/(he|l|o)/, {l: "la"}).should be(str)
1008+
end
1009+
10041010
it "subs using $~" do
10051011
"foo".sub(/(o)/) { "x#{$1}x" }.should eq("fxoxo")
10061012
end

Diff for: ‎src/string.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1423,7 +1423,7 @@ class String
14231423
# "hello".sub(/(he|l|o)/, {"he": "ha", "l": "la"}) # => "hallo"
14241424
# "hello".sub(/(he|l|o)/, {"l": "la"}) # => "hello"
14251425
# ```
1426-
def sub(pattern : Regex, hash : Hash(String, _))
1426+
def sub(pattern : Regex, hash : Hash(String, _) | NamedTuple)
14271427
sub(pattern) { |match|
14281428
if hash.has_key?(match)
14291429
hash[match]

0 commit comments

Comments
 (0)
Please sign in to comment.