= FileStr
= Introduction
FileStr is a library that augments String objects with File, Dir, and
FileUtils capabilities (class methods). The String (self) is passed as
the first argument to original class methods. With FileStr you can use
"normal" strings as filename/dirname objects.
It is sometimes inconvenient to write
File.exist?( 'report.txt' )
when you could instead do
'report.txt'.f_exist?
The class methods from File are prefixed with "f_", Dir methods with
"d_", and FileUtils methods with "u_". There are some special versions
of methods which are prefixed with "s_" (See: Methods).
FileStr class inherits String class, i.e. it has all String
methods. It also includes all the augmenting methods. You can keep
the augmenting methods within FileStr class, or you can explicitly
make them also part of String class.
= Usage
After requiring the "filestr" library, you have FileStr class defined
which includes methods from File, Dir, and FileUtils.
You can create a FileStr object from String:
require 'filestr'
fs = 'report.txt'.to_fs
or
fs = 'report.txt'.fs
Then you can check if the file exists?
fs.f_exist?
If you execute:
String.fileStr
you get all augmenting methods straight to String class and you can
use strings directly. For example:
'report.txt'.f_exist?
or
'bin'.d_exist?
to test if there is a sub-directory called "bin".
You can rename (move) a file with:
'report.txt'.u_mv( 'history.txt' )
= Methods
Almost all class methods from File and Dir are imported. A filtered
selection is taken from FileUtils.
Some methods from File and Dir doesn't take a name (filename, dirname)
as the first argument. There are modified versions of these methods
within FileStr where the "name" argument comes from self. These method
are prefixed with "s_".
List of special methods:
s_chmod, s_chown, s_fnmatch
You can change a mode of a file:
'report.txt'.s_chmod( 0777 )
There is also a special version of Dir.glob, called s_glob. s_glob
concatenates the string (self) to the given "pattern" argument, and
passed this to Dir.glob.
The complete list of methods coming directly from File, Dir, and
FileUtils is:
f_binread, f_binwrite, f_copy_stream, f_foreach, f_read,
f_readlines, f_sysopen, f_write, f_absolute_path, f_atime,
f_basename, f_birthtime, f_blockdev?, f_chardev?, f_ctime,
f_delete, f_directory?, f_dirname, f_executable?,
f_executable_real?, f_exist?, f_exists?, f_expand_path,
f_extname, f_filename, f_file?, f_ftype, f_grpowned?,
f_identical?, f_join, f_link, f_lstat, f_mtime, f_new, f_open,
f_owned?, f_path, f_pipe?, f_readable?, f_readable_real?,
f_readlink, f_realdirpath, f_realpath, f_rename, f_setgid?,
f_setuid?, f_size, f_size?, f_socket?, f_split, f_stat,
f_sticky?, f_symlink, f_symlink?, f_truncate, f_unlink,
f_world_readable?, f_world_writable?, f_writable?,
f_writable_real?, f_zero?, d_chdir, d_chroot, d_delete,
d_entries, d_exist?, d_exists?, d_foreach, d_mkdir, d_new,
d_open, d_rmdir, d_unlink, u_cd, u_cmp, u_mkdir_p, u_ln_s,
u_cp, u_cp_r, u_mv, u_rm, u_rm_f, u_rm_r, u_rm_rf, u_touch,
u_uptodate?
You can use the original documentation of the class methods
above. Just imagine the string (self) as being the first argument, and
the rest are as in documentation.