Skip to content

Commit

Permalink
Add String#sub(Regex, NamedTuple)
Browse files Browse the repository at this point in the history
  • Loading branch information
maiha authored and asterite committed Jan 4, 2017
1 parent d4ef40c commit 4fbab30
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
6 changes: 6 additions & 0 deletions spec/std/string_spec.cr
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/string.cr
Expand Up @@ -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]
Expand Down

0 comments on commit 4fbab30

Please sign in to comment.