Class

File

Inheritance

ftools.rb: Extra tools for the File class

Author:WATANABE, Hirofumi
Documentation:Zachary Landau

This library can be distributed under the terms of the Ruby license. You can freely distribute/modify this library.

It is included in the Ruby standard library.

Description

ftools adds several (class, not instance) methods to the File class, for copying, moving, deleting, installing, and comparing files, as well as creating a directory path. See the File class for details.

FileUtils contains all or nearly all the same functionality and more, and is a recommended option over ftools

When you

  require 'ftools'

then the File class aquires some utility methods for copying, moving, and deleting files, and more.

See the method descriptions below, and consider using FileUtils as it is more comprehensive.

Classes & Modules

Constants

Name   Description
ALT_SEPARATOR = Qnil
ALT_SEPARATOR = rb_obj_freeze(rb_str_new2("\\"))
BUFSIZE = 8 * 1024
PATH_SEPARATOR = rb_obj_freeze(rb_str_new2(PATH_SEP))
SEPARATOR = separator
Separator = separator

Aliases

Method Alias Description
compare → cmp
copy → cp
makedirs → mkpath
move → mv
safe_unlink → rm_f

Methods

Class

Visibility Signature
public atime (p1)
public basename (...)
public blockdev? (p1)
public catname (from, to)
public chardev? (p1)
public chmod (...)
public chmod (mode, *files)
public chown (...)
public compare (from, to, verbose = false)
public copy (from, to, verbose = false)
public ctime (p1)
public delete (...)
public directory? (p1)
public dirname (p1)
public executable? (p1)
public executable_real? (p1)
public exist? (p1)
public exists? (p1)
public expand_path (...)
public extname (p1)
public file? (p1)
public fnmatch (...)
public fnmatch? (...)
public ftype (p1)
public grpowned? (p1)
public identical? (p1, p2)
public install (from, to, mode = nil, verbose = false)
public join (...)
public lchmod (...)
public lchown (...)
public link (p1, p2)
public lstat (p1)
public makedirs (*dirs)
public move (from, to, verbose = false)
public mtime (p1)
public new (...)
public owned? (p1)
public pipe? (p1)
public readable? (p1)
public readable_real? (p1)
public readlink (p1)
public rename (p1, p2)
public safe_unlink (*files)
public setgid? (p1)
public setuid? (p1)
public size (p1)
public size? (p1)
public socket? (p1)
public split (p1)
public stat (p1)
public sticky? (p1)
public symlink (p1, p2)
public symlink? (p1)
public syscopy (from, to)
public truncate (p1, p2)
public umask (...)
public unlink (...)
public utime (...)
public writable? (p1)
public writable_real? (p1)
public zero? (p1)

Instance

Visibility Signature
public atime ()
public chmod (p1)
public chown (p1, p2)
public ctime ()
public flock (p1)
public lstat ()
public mtime ()
public o_chmod (p1)
public path ()
public truncate (p1)

Class Method Detail

File.atime(file_name) => time

Returns the last access time for the named file as a Time object).

   File.atime("testfile")   #=> Wed Apr 09 08:51:48 CDT 2003

File.basename(file_name [, suffix] ) → base_name

Returns the last component of the filename given in file_name, which must be formed using forward slashes (``/’’) regardless of the separator used on the local file system. If suffix is given and present at the end of file_name, it is removed.

   File.basename("/home/gumby/work/ruby.rb")          #=> "ruby.rb"
   File.basename("/home/gumby/work/ruby.rb", ".rb")   #=> "ruby"

File.blockdev?(file_name) => true or false

Returns true if the named file is a block device.

catname(from, to)

If to is a valid directory, from will be appended to to, adding and escaping backslashes as necessary. Otherwise, to will be returned. Useful for appending from to to only if the filename was not specified in to.

File.chardev?(file_name) => true or false

Returns true if the named file is a character device.

File.chmod(mode_int, file_name, ... ) → integer

Changes permission bits on the named file(s) to the bit pattern represented by mode_int. Actual effects are operating system dependent (see the beginning of this section). On Unix systems, see chmod(2) for details. Returns the number of files processed.

   File.chmod(0644, "testfile", "out")   #=> 2

chmod(mode, *files)

Changes permission bits on files to the bit pattern represented by mode. If the last parameter isn‘t a String, verbose mode will be enabled.

  File.chmod 0755, 'somecommand'
  File.chmod 0644, 'my.rb', 'your.rb', true

File.chown(owner_int, group_int, file_name,... ) → integer

Changes the owner and group of the named file(s) to the given numeric owner and group id‘s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file‘s group to any group to which the owner belongs. A nil or -1 owner or group id is ignored. Returns the number of files processed.

   File.chown(nil, 100, "testfile")

compare(from, to, verbose = false)

Returns true if and only if the contents of files from and to are identical. If verbose is true, from <=> to is printed.

copy(from, to, verbose = false)

Copies a file from to to using syscopy. If to is a directory, copies from to to/from. If verbose is true, from -> to is printed.

File.ctime(file_name) => time

Returns the change time for the named file (the time at which directory information about the file was changed, not the file itself).

   File.ctime("testfile")   #=> Wed Apr 09 08:53:13 CDT 2003

File.delete(file_name, ...) => integer
File.unlink(file_name, ...) => integer

Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir.

File.directory?(file_name) => true or false

Returns true if the named file is a directory, false otherwise.

   File.directory?(".")

File.dirname(file_name ) → dir_name

Returns all components of the filename given in file_name except the last one. The filename must be formed using forward slashes (``/’’) regardless of the separator used on the local file system.

   File.dirname("/home/gumby/work/ruby.rb")   #=> "/home/gumby/work"

File.executable?(file_name) => true or false

Returns true if the named file is executable by the effective user id of this process.

File.executable_real?(file_name) => true or false

Returns true if the named file is executable by the real user id of this process.

File.exist?(file_name) => true or false
File.exists?(file_name) => true or false (obsolete)

Return true if the named file exists.

File.exist?(file_name) => true or false
File.exists?(file_name) => true or false (obsolete)

Return true if the named file exists.

File.expand_path(file_name [, dir_string] ) → abs_file_name

Converts a pathname to an absolute pathname. Relative paths are referenced from the current working directory of the process unless dir_string is given, in which case it will be used as the starting point. The given pathname may start with a ``~’’, which expands to the process owner‘s home directory (the environment variable HOME must be set correctly). ``~user’’ expands to the named user‘s home directory.

   File.expand_path("~oracle/bin")           #=> "/home/oracle/bin"
   File.expand_path("../../bin", "/tmp/x")   #=> "/bin"

File.extname(path) → string

Returns the extension (the portion of file name in path after the period).

   File.extname("test.rb")         #=> ".rb"
   File.extname("a/b/d/test.rb")   #=> ".rb"
   File.extname("test")            #=> ""
   File.extname(".profile")        #=> ""

File.file?(file_name) => true or false

Returns true if the named file exists and is a regular file.

File.fnmatch( pattern, path, [flags] ) => (true or false)
File.fnmatch?( pattern, path, [flags] ) => (true or false)

Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:

*:Matches any file. Can be restricted by other values in the glob. * will match all files; c* will match all files beginning with c; *c will match all files ending with c; and c will match all files that have c in them (including at the beginning or end). Equivalent to / .* /x in regexp.
**:Matches directories recursively or files expansively.
?:Matches any one character. Equivalent to /.{1}/ in regexp.
[set]:Matches any one character in set. Behaves exactly like character sets in Regexp, including set negation ([^a-z]).
<code></code>:Escapes the next metacharacter.

flags is a bitwise OR of the FNM_xxx parameters. The same glob pattern and flags are used by Dir::glob.

   File.fnmatch('cat',       'cat')        #=> true  : match entire string
   File.fnmatch('cat',       'category')   #=> false : only match partial string
   File.fnmatch('c{at,ub}s', 'cats')       #=> false : { } isn't supported

   File.fnmatch('c?t',     'cat')          #=> true  : '?' match only 1 character
   File.fnmatch('c??t',    'cat')          #=> false : ditto
   File.fnmatch('c*',      'cats')         #=> true  : '*' match 0 or more characters
   File.fnmatch('c*t',     'c/a/b/t')      #=> true  : ditto
   File.fnmatch('ca[a-z]', 'cat')          #=> true  : inclusive bracket expression
   File.fnmatch('ca[^t]',  'cat')          #=> false : exclusive bracket expression ('^' or '!')

   File.fnmatch('cat', 'CAT')                     #=> false : case sensitive
   File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true  : case insensitive

   File.fnmatch('?',   '/', File::FNM_PATHNAME)  #=> false : wildcard doesn't match '/' on FNM_PATHNAME
   File.fnmatch('*',   '/', File::FNM_PATHNAME)  #=> false : ditto
   File.fnmatch('[/]', '/', File::FNM_PATHNAME)  #=> false : ditto

   File.fnmatch('\?',   '?')                       #=> true  : escaped wildcard becomes ordinary
   File.fnmatch('\a',   'a')                       #=> true  : escaped ordinary remains ordinary
   File.fnmatch('\a',   '\a', File::FNM_NOESCAPE)  #=> true  : FNM_NOESACPE makes '\' ordinary
   File.fnmatch('[\?]', '?')                       #=> true  : can escape inside bracket expression

   File.fnmatch('*',   '.profile')                      #=> false : wildcard doesn't match leading
   File.fnmatch('*',   '.profile', File::FNM_DOTMATCH)  #=> true    period by default.
   File.fnmatch('.*',  '.profile')                      #=> true

   rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
   File.fnmatch(rbfiles, 'main.rb')                    #=> false
   File.fnmatch(rbfiles, './main.rb')                  #=> false
   File.fnmatch(rbfiles, 'lib/song.rb')                #=> true
   File.fnmatch('**.rb', 'main.rb')                    #=> true
   File.fnmatch('**.rb', './main.rb')                  #=> false
   File.fnmatch('**.rb', 'lib/song.rb')                #=> true
   File.fnmatch('*',           'dave/.profile')                      #=> true

   pattern = '*' '/' '*'
   File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME)  #=> false
   File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true

   pattern = '**' '/' 'foo'
   File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME)     #=> true
   File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME)    #=> true
   File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME)  #=> true
   File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME)    #=> false
   File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true

File.fnmatch( pattern, path, [flags] ) => (true or false)
File.fnmatch?( pattern, path, [flags] ) => (true or false)

Returns true if path matches against pattern The pattern is not a regular expression; instead it follows rules similar to shell filename globbing. It may contain the following metacharacters:

*:Matches any file. Can be restricted by other values in the glob. * will match all files; c* will match all files beginning with c; *c will match all files ending with c; and c will match all files that have c in them (including at the beginning or end). Equivalent to / .* /x in regexp.
**:Matches directories recursively or files expansively.
?:Matches any one character. Equivalent to /.{1}/ in regexp.
[set]:Matches any one character in set. Behaves exactly like character sets in Regexp, including set negation ([^a-z]).
<code></code>:Escapes the next metacharacter.

flags is a bitwise OR of the FNM_xxx parameters. The same glob pattern and flags are used by Dir::glob.

   File.fnmatch('cat',       'cat')        #=> true  : match entire string
   File.fnmatch('cat',       'category')   #=> false : only match partial string
   File.fnmatch('c{at,ub}s', 'cats')       #=> false : { } isn't supported

   File.fnmatch('c?t',     'cat')          #=> true  : '?' match only 1 character
   File.fnmatch('c??t',    'cat')          #=> false : ditto
   File.fnmatch('c*',      'cats')         #=> true  : '*' match 0 or more characters
   File.fnmatch('c*t',     'c/a/b/t')      #=> true  : ditto
   File.fnmatch('ca[a-z]', 'cat')          #=> true  : inclusive bracket expression
   File.fnmatch('ca[^t]',  'cat')          #=> false : exclusive bracket expression ('^' or '!')

   File.fnmatch('cat', 'CAT')                     #=> false : case sensitive
   File.fnmatch('cat', 'CAT', File::FNM_CASEFOLD) #=> true  : case insensitive

   File.fnmatch('?',   '/', File::FNM_PATHNAME)  #=> false : wildcard doesn't match '/' on FNM_PATHNAME
   File.fnmatch('*',   '/', File::FNM_PATHNAME)  #=> false : ditto
   File.fnmatch('[/]', '/', File::FNM_PATHNAME)  #=> false : ditto

   File.fnmatch('\?',   '?')                       #=> true  : escaped wildcard becomes ordinary
   File.fnmatch('\a',   'a')                       #=> true  : escaped ordinary remains ordinary
   File.fnmatch('\a',   '\a', File::FNM_NOESCAPE)  #=> true  : FNM_NOESACPE makes '\' ordinary
   File.fnmatch('[\?]', '?')                       #=> true  : can escape inside bracket expression

   File.fnmatch('*',   '.profile')                      #=> false : wildcard doesn't match leading
   File.fnmatch('*',   '.profile', File::FNM_DOTMATCH)  #=> true    period by default.
   File.fnmatch('.*',  '.profile')                      #=> true

   rbfiles = '**' '/' '*.rb' # you don't have to do like this. just write in single string.
   File.fnmatch(rbfiles, 'main.rb')                    #=> false
   File.fnmatch(rbfiles, './main.rb')                  #=> false
   File.fnmatch(rbfiles, 'lib/song.rb')                #=> true
   File.fnmatch('**.rb', 'main.rb')                    #=> true
   File.fnmatch('**.rb', './main.rb')                  #=> false
   File.fnmatch('**.rb', 'lib/song.rb')                #=> true
   File.fnmatch('*',           'dave/.profile')                      #=> true

   pattern = '*' '/' '*'
   File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME)  #=> false
   File.fnmatch(pattern, 'dave/.profile', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true

   pattern = '**' '/' 'foo'
   File.fnmatch(pattern, 'a/b/c/foo', File::FNM_PATHNAME)     #=> true
   File.fnmatch(pattern, '/a/b/c/foo', File::FNM_PATHNAME)    #=> true
   File.fnmatch(pattern, 'c:/a/b/c/foo', File::FNM_PATHNAME)  #=> true
   File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME)    #=> false
   File.fnmatch(pattern, 'a/.b/c/foo', File::FNM_PATHNAME | File::FNM_DOTMATCH) #=> true

File.ftype(file_name) => string

Identifies the type of the named file; the return string is one of ``file’’, ``directory’’, ``characterSpecial’’, ``blockSpecial’’, ``fifo’’, ``link’’, ``socket’’, or ``unknown’’.

   File.ftype("testfile")            #=> "file"
   File.ftype("/dev/tty")            #=> "characterSpecial"
   File.ftype("/tmp/.X11-unix/X0")   #=> "socket"

File.grpowned?(file_name) => true or false

Returns true if the named file exists and the effective group id of the calling process is the owner of the file. Returns false on Windows.

File.identical?(file_1, file_2) => true or false

Returns true if the named files are identical.

    open("a", "w") {}
    p File.identical?("a", "a")      #=> true
    p File.identical?("a", "./a")    #=> true
    File.link("a", "b")
    p File.identical?("a", "b")      #=> true
    File.symlink("a", "c")
    p File.identical?("a", "c")      #=> true
    open("d", "w") {}
    p File.identical?("a", "d")      #=> false

install(from, to, mode = nil, verbose = false)

If src is not the same as dest, copies it and changes the permission mode to mode. If dest is a directory, destination is dest/src. If mode is not set, default is used. If verbose is set to true, the name of each file copied will be printed.

File.join(string, ...) → path

Returns a new string formed by joining the strings using File::SEPARATOR.

   File.join("usr", "mail", "gumby")   #=> "usr/mail/gumby"

File.lchmod(mode_int, file_name, ...) => integer

Equivalent to File::chmod, but does not follow symbolic links (so it will change the permissions associated with the link, not the file referenced by the link). Often not available.

file.lchown(owner_int, group_int, file_name,..) => integer

Equivalent to File::chown, but does not follow symbolic links (so it will change the owner associated with the link, not the file referenced by the link). Often not available. Returns number of files in the argument list.

File.link(old_name, new_name) => 0

Creates a new name for an existing file using a hard link. Will not overwrite new_name if it already exists (raising a subclass of SystemCallError). Not available on all platforms.

   File.link("testfile", ".testfile")   #=> 0
   IO.readlines(".testfile")[0]         #=> "This is line one\n"

File.lstat(file_name) => stat

Same as File::stat, but does not follow the last symbolic link. Instead, reports on the link itself.

   File.symlink("testfile", "link2test")   #=> 0
   File.stat("testfile").size              #=> 66
   File.lstat("link2test").size            #=> 8
   File.stat("link2test").size             #=> 66

makedirs(*dirs)

Creates a directory and all its parent directories. For example,

    File.makedirs '/usr/lib/ruby'

causes the following directories to be made, if they do not exist.

    * /usr
    * /usr/lib
    * /usr/lib/ruby

You can pass several directories, each as a parameter. If the last parameter isn‘t a String, verbose mode will be enabled.

move(from, to, verbose = false)

Moves a file from to to using syscopy. If to is a directory, copies from from to to/from. If verbose is true, from -> to is printed.

File.mtime(file_name) => time

Returns the modification time for the named file as a Time object.

   File.mtime("testfile")   #=> Tue Apr 08 12:58:04 CDT 2003

File.new(filename, mode="r") => file
File.new(filename [, mode [, perm]]) => file

Opens the file named by filename according to mode (default is ``r’’) and returns a new File object. See the description of class IO for a description of mode. The file mode may optionally be specified as a Fixnum by or-ing together the flags (O_RDONLY etc, again described under IO). Optional permission bits may be given in perm. These mode and permission bits are platform dependent; on Unix systems, see open(2) for details.

   f = File.new("testfile", "r")
   f = File.new("newfile",  "w+")
   f = File.new("newfile", File::CREAT|File::TRUNC|File::RDWR, 0644)

File.owned?(file_name) => true or false

Returns true if the named file exists and the effective used id of the calling process is the owner of the file.

File.pipe?(file_name) => true or false

Returns true if the named file is a pipe.

File.readable?(file_name) => true or false

Returns true if the named file is readable by the effective user id of this process.

File.readable_real?(file_name) => true or false

Returns true if the named file is readable by the real user id of this process.

File.readlink(link_name) → file_name

Returns the name of the file referenced by the given link. Not available on all platforms.

   File.symlink("testfile", "link2test")   #=> 0
   File.readlink("link2test")              #=> "testfile"

File.rename(old_name, new_name) => 0

Renames the given file to the new name. Raises a SystemCallError if the file cannot be renamed.

   File.rename("afile", "afile.bak")   #=> 0

safe_unlink(*files)

Removes a list of files. Each parameter should be the name of the file to delete. If the last parameter isn‘t a String, verbose mode will be enabled. Returns the number of files deleted.

File.setgid?(file_name) => true or false

Returns true if the named file has the setgid bit set.

File.setuid?(file_name) => true or false

Returns true if the named file has the setuid bit set.

File.size(file_name) => integer

Returns the size of file_name.

File.size?(file_name) => Integer or nil

Returns nil if file_name doesn‘t exist or has zero size, the size of the file otherwise.

File.socket?(file_name) => true or false

Returns true if the named file is a socket.

File.split(file_name) => array

Splits the given string into a directory and a file component and returns them in a two-element array. See also File::dirname and File::basename.

   File.split("/home/gumby/.profile")   #=> ["/home/gumby", ".profile"]

File.stat(file_name) => stat

Returns a File::Stat object for the named file (see File::Stat).

   File.stat("testfile").mtime   #=> Tue Apr 08 12:58:04 CDT 2003

File.sticky?(file_name) => true or false

Returns true if the named file has the sticky bit set.

File.symlink(old_name, new_name) => 0

Creates a symbolic link called new_name for the existing file old_name. Raises a NotImplemented exception on platforms that do not support symbolic links.

   File.symlink("testfile", "link2test")   #=> 0

File.symlink?(file_name) => true or false

Returns true if the named file is a symbolic link.

syscopy(from, to)

Copies a file from to to. If to is a directory, copies from to to/from.

File.truncate(file_name, integer) => 0

Truncates the file file_name to be at most integer bytes long. Not available on all platforms.

   f = File.new("out", "w")
   f.write("1234567890")     #=> 10
   f.close                   #=> nil
   File.truncate("out", 5)   #=> 0
   File.size("out")          #=> 5

File.umask() => integer
File.umask(integer) => integer

Returns the current umask value for this process. If the optional argument is given, set the umask to that value and return the previous value. Umask values are subtracted from the default permissions, so a umask of 0222 would make a file read-only for everyone.

   File.umask(0006)   #=> 18
   File.umask         #=> 6

File.delete(file_name, ...) => integer
File.unlink(file_name, ...) => integer

Deletes the named files, returning the number of names passed as arguments. Raises an exception on any error. See also Dir::rmdir.

File.utime(atime, mtime, file_name,...) => integer

Sets the access and modification times of each named file to the first two arguments. Returns the number of file names in the argument list.

File.writable?(file_name) => true or false

Returns true if the named file is writable by the effective user id of this process.

File.writable_real?(file_name) => true or false

Returns true if the named file is writable by the real user id of this process.

File.zero?(file_name) => true or false

Returns true if the named file exists and has a zero size.

Instance Method Detail

file.atime => time

Returns the last access time (a Time object)

 for <i>file</i>, or epoch if <i>file</i> has not been accessed.

   File.new("testfile").atime   #=> Wed Dec 31 18:00:00 CST 1969

file.chmod(mode_int) => 0

Changes permission bits on file to the bit pattern represented by mode_int. Actual effects are platform dependent; on Unix systems, see chmod(2) for details. Follows symbolic links. Also see File#lchmod.

   f = File.new("out", "w");
   f.chmod(0644)   #=> 0

file.chown(owner_int, group_int ) => 0

Changes the owner and group of file to the given numeric owner and group id‘s. Only a process with superuser privileges may change the owner of a file. The current owner of a file may change the file‘s group to any group to which the owner belongs. A nil or -1 owner or group id is ignored. Follows symbolic links. See also File#lchown.

   File.new("testfile").chown(502, 1000)

file.ctime → time

Returns the change time for file (that is, the time directory information about the file was changed, not the file itself).

   File.new("testfile").ctime   #=> Wed Apr 09 08:53:14 CDT 2003

file.flock (locking_constant ) => 0 or false

Locks or unlocks a file according to locking_constant (a logical or of the values in the table below). Returns false if File::LOCK_NB is specified and the operation would otherwise have blocked. Not available on all platforms.

Locking constants (in class File):

   LOCK_EX   | Exclusive lock. Only one process may hold an
             | exclusive lock for a given file at a time.
   ----------+------------------------------------------------
   LOCK_NB   | Don't block when locking. May be combined
             | with other lock options using logical or.
   ----------+------------------------------------------------
   LOCK_SH   | Shared lock. Multiple processes may each hold a
             | shared lock for a given file at the same time.
   ----------+------------------------------------------------
   LOCK_UN   | Unlock.

Example:

   File.new("testfile").flock(File::LOCK_UN)   #=> 0

file.lstat => stat

Same as IO#stat, but does not follow the last symbolic link. Instead, reports on the link itself.

   File.symlink("testfile", "link2test")   #=> 0
   File.stat("testfile").size              #=> 66
   f = File.new("link2test")
   f.lstat.size                            #=> 8
   f.stat.size                             #=> 66

file.mtime → time

Returns the modification time for file.

   File.new("testfile").mtime   #=> Wed Apr 09 08:53:14 CDT 2003

o_chmod(p1)

Alias for chmod

file.path → filename

Returns the pathname used to create file as a string. Does not normalize the name.

   File.new("testfile").path               #=> "testfile"
   File.new("/tmp/../tmp/xxx", "w").path   #=> "/tmp/../tmp/xxx"

file.truncate(integer) => 0

Truncates file to at most integer bytes. The file must be opened for writing. Not available on all platforms.

   f = File.new("out", "w")
   f.syswrite("1234567890")   #=> 10
   f.truncate(5)              #=> 0
   f.close()                  #=> nil
   File.size("out")           #=> 5