Skip to content

Commit 8c5babc

Browse files
committed
[GR-18163] File#path should return a new mutable String on every call
PullRequest: truffleruby/2047
2 parents c44a6de + 660c5e5 commit 8c5babc

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Compatibility:
4343
* Implement negative line numbers for eval (#1482).
4444
* Support refinements for `#to_s` called by string interpolation (#2110, @ssnickolay)
4545
* Module#using raises error in method scope (#2112, @ssnickolay)
46+
* `File#path` now returns a new mutable String on every call like MRI (#2115).
4647

4748
Performance:
4849

spec/ruby/core/file/shared/path.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,23 @@
1515
@file.send(@method).should be_an_instance_of(String)
1616
end
1717

18+
it "returns a different String on every call" do
19+
@file = File.new @path
20+
path1 = @file.send(@method)
21+
path2 = @file.send(@method)
22+
path1.should == path2
23+
path1.should_not.equal?(path2)
24+
end
25+
26+
it "returns a mutable String" do
27+
@file = File.new @path.dup.freeze
28+
path = @file.send(@method)
29+
path.should == @path
30+
path.should_not.frozen?
31+
path << "test"
32+
@file.send(@method).should == @path
33+
end
34+
1835
it "calls to_str on argument and returns exact value" do
1936
path = mock('path')
2037
path.should_receive(:to_str).and_return(@path)

src/main/ruby/truffleruby/core/file.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ module Constants
108108
PATH_SEPARATOR = ':'
109109
POSIX = Truffle::POSIX
110110

111-
attr_reader :path
112-
113111
# The mode_t type is 2 bytes (ushort). Instead of getting whatever
114112
# value happens to be in the least significant 16 bits, just set
115113
# the value to 0 if it is greater than 0xffff. Also, negative values
@@ -1238,6 +1236,9 @@ def stat
12381236
Stat.fstat Primitive.io_fd(self)
12391237
end
12401238

1239+
def path
1240+
@path.dup
1241+
end
12411242
alias_method :to_path, :path
12421243

12431244
def truncate(length)

0 commit comments

Comments
 (0)