stdlib : file

Methods for manipulating files.
close   delete   each_line   exists?   make_dir   open   read   read_line   read_lines   rename   type   write   write_line  

close

file.close

Closes the file.

delete

file.delete path

Deletes the file at the given path.

each_line

file.each_line path { line block }

Opens the file a the given path, calling the block for each line of the file.

each_line

file.each_line { line block }

Calls the block, passing in each line of the file as an argument, then closes the file.

exists?

file.exists? path

Returns true if the given path exists.

make_dir

file.make_dir path

Makes a new directory at the given path.

open

file.open path
file.open path, mode
file.open path, { file | block }
file.open path, mode, { file | block }

Opens a file at the given path. If no mode is given, the default is “r” (read-only).

Possible modes:

  • “r”: read mode (the default)
  • “w”: write mode
  • “a”: append mode
  • “r+”: update mode, all previous data is preserved
  • “w+”: update mode, all previous data is erased
  • “a+”: append update mode, previous data is preserved, writing is only allowed at the end of file

If no block is given, this returns the open file handle.

If a block is given, the file handle is passed to the block. When the block is finished, the file will be closed.

read

file.read path

Read entire file into a string.

read_line

file.read_line

Reads a single line from the file and returns it as a string.

read_lines

file.read_lines path

Reads the file into an array of lines.

rename

file.rename original, new

Rename a file.

type

file.type path

Return the type of a file as a string.

Type can be

  • file
  • directory
  • link
  • socket
  • named pipe
  • char device
  • block device
  • other

write

file.write data

Write data to the file.

write_line

file.write_line data

Write data to the file, followed by “\n”.

Fork me on GitHub