Path is a library to manage paths.
It is similar to Pathname, but has some extra goodness.
The method names are intended to be short and explicit, and avoid too much duplication like having 'name' or 'path' in the method name.
I believe the object-oriented approach to manipulate paths is very elegant and useful.
Paths are naturally the subject of their methods and even if they are simple Strings behind, they carry way much more information and deserve a first-class status.
Also, using a path library like this avoid to remember in which class the functionality is implemented, everything is in one place (if not, please open an issue!).
gem install epath
See the Path class documentation for details.
All the useful methods of File (and so IO) and Dir should be included.
Most methods of FileUtils should be there too.
Path.new('/usr/bin')
Path['/usr/bin']
Path('/usr/bin') # unless NO_EPATH_GLOBAL_FUNCTION is defined
Path.new('~myuser/path') # expanded if it begins with ~
# Separators are replaced by / on systems having File::ALT_SEPARATOR
Path.new('win\sepa\rator') # => #<Path win/sepa/rator>
Path.new('/usr', 'bin')
%w[foo bar].map(&Path) # => [Path('foo'), Path('bar')]Path.file # == Path(__FILE__).expand
Path.dir # == Path(File.dirname(__FILE__)).expand
Path.relative(path) # == Path(File.expand_path("../#{path}", __FILE__))
Path.home or Path.~ # == Path(File.expand_path('~'))
Path.~(user) # == Path(File.expand_path("~#{user}"))Path.tmpfile
Path.tmpdir- expand => expand_path
- relative_to => relative_path_from
Path can split a path in two ways:
The first way is the one done by File methods (dirname, basename, extname).
The second is Path's own way in which the base is given without the extension and the extension is given without the leading dot.
The rationale behind this is to have a true three-components path, splitting on the / and the . (See this issue for details)
dirname basename
____________ ______
/ \ / \
/some/path/dir/file.ext
\____________/ \__/ \_/
dir base ext
path = dirname / basename
path = dirname / basename(extname) extname
path = dir / base [. ext]
- dirname: "/some/path/dir"
- basename: "file.ext"
- extname: ".ext"
- dir: alias of dirname: "/some/paths/dir"
- base: basename(extname), the basename without the extension: "file"
- ext: extname without the leading dot: "ext"
- join(*parts)
- /: join paths (as Pathname#+)
Path('/usr')/'bin'- add_ext / add_extension
- rm_ext / without_extension
- sub_ext(new_ext) / replace_extension(new_ext)
- children: files under self, without . and ..
- glob: relative glob to self, yield absolute paths
- parent: parent directory (don't use #dirname more than once, use #parent instead)
- ascend, ancestors: self and all the parent directories
- descend: in the reverse order
- backfind: ascends the parents until it finds the given path
# Path.backfind is Path.here.backfind
Path.backfind('lib') # => Path's lib folder
# It accepts XPath-like context
Path.backfind('.[.git]') # => the root of this repository- read
- write(contents)
- append(contents)
- mkdir
- mkdir_p
- rm_rf
- Path.require_tree: require all .rb files recursively (in alphabetic order)
This is still in the early development stage, you should expect many additions and some changes.
Benoit Daloze - eregon
Bernard Lambeau - blambeau
Ravil Bayramgalin - brainopia