Skip to content

Commit

Permalink
Add File#rename (#13640)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlhoerberg authored Jul 22, 2023
1 parent 55eefa4 commit 56e32f6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spec/std/file_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,16 @@ describe "File" do
end
end
end

it "renames a File instance" do
with_tempfile("rename-source.txt", "rename-target.txt") do |source_path, target_path|
f = File.new(source_path, "w")
f.rename target_path
f.path.should eq target_path
File.exists?(source_path).should be_false
File.exists?(target_path).should be_true
end
end
end

# There are more detailed specs for `Path#expand` in path_spec.cr
Expand Down
6 changes: 6 additions & 0 deletions src/file.cr
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,12 @@ class File < IO::FileDescriptor
end
end

# Rename the current `File`
def rename(new_filename : Path | String) : Nil
File.rename(@path, new_filename)
@path = new_filename.to_s
end

# Sets the access and modification times of *filename*.
#
# Use `#utime` if the `File` is already open.
Expand Down

0 comments on commit 56e32f6

Please sign in to comment.