Skip to content

Commit

Permalink
String#dup returns copy when no modification made. Fixes #3633.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Apr 4, 2016
1 parent cfd3f08 commit 42fb295
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/string.rb
Expand Up @@ -1388,7 +1388,7 @@ def sub(pattern, replacement=undefined)
ret.append(match.post_match)
tainted ||= val.tainted?
else
return self
ret = dup
end

ret.taint if tainted
Expand Down
10 changes: 9 additions & 1 deletion spec/ruby/core/string/sub_spec.rb
Expand Up @@ -2,6 +2,14 @@
require File.expand_path('../fixtures/classes.rb', __FILE__)

describe "String#sub with pattern, replacement" do
it "returns a copy of self when no modification is made" do
a = "hello"
b = a.sub /w.*$/, "*"

b.should_not equal(a)
b.should == "hello"
end

it "returns a copy of self with all occurrences of pattern replaced with replacement" do
"hello".sub(/[aeiou]/, '*').should == "h*llo"
"hello".sub(//, ".").should == ".hello"
Expand Down Expand Up @@ -562,4 +570,4 @@
it "raises a ArgumentError" do
lambda { "abca".sub!(/a/) }.should raise_error(ArgumentError)
end
end
end

0 comments on commit 42fb295

Please sign in to comment.