diff --git a/src/file.cr b/src/file.cr index 1f2e2d54728a..9e3a4a7e88cc 100644 --- a/src/file.cr +++ b/src/file.cr @@ -136,9 +136,9 @@ class File < IO::FileDescriptor # # File.symlink("foo", "bar") # File.info?("bar", follow_symlinks: false).try(&.type.symlink?) # => true - # - # Use `#info` if the `File` is already open. # ``` + # + # Use `IO::FileDescriptor#info` if the file is already open. def self.info?(path : Path | String, follow_symlinks = true) : Info? Crystal::System::File.info?(path.to_s, follow_symlinks) end @@ -156,9 +156,9 @@ class File < IO::FileDescriptor # # File.symlink("foo", "bar") # File.info("bar", follow_symlinks: false).type.symlink? # => true - # - # Use `#info` if the `File` is already open. # ``` + # + # Use `IO::FileDescriptor#info` if the file is already open. def self.info(path : Path | String, follow_symlinks = true) : Info Crystal::System::File.info(path.to_s, follow_symlinks) end diff --git a/src/io/file_descriptor.cr b/src/io/file_descriptor.cr index 09a37ac54ffa..444c5a20bbc5 100644 --- a/src/io/file_descriptor.cr +++ b/src/io/file_descriptor.cr @@ -61,7 +61,25 @@ class IO::FileDescriptor < IO end {% end %} - def info + # Returns a `File::Info` object for this file descriptor, or raises + # `IO::Error` in case of an error. + # + # Certain fields like the file size may not be updated until an explicit + # flush. + # + # ``` + # File.write("testfile", "abc") + # + # file = File.new("testfile", "a") + # file.info.size # => 3 + # file << "defgh" + # file.info.size # => 3 + # file.flush + # file.info.size # => 8 + # ``` + # + # Use `File.info` if the file is not open and a path to the file is available. + def info : File::Info system_info end