Class: Pathname
Class Method Summary collapse
-
.getwd ⇒ Object
Pathname.new(Dir.getwd).
-
.tempfile ⇒ Object
Creates a new temp file using Tempfile.new and returns the Pathname object for that file.
Instance Method Summary collapse
-
#absolutize ⇒ Object
Returns a Pathname object representing the absolute path equivalent to self.
- #add_prefix(s) ⇒ Object
-
#add_suffix(s) ⇒ Object
This is needed since <#Pathname> + <#String> treats both self and the string as a directory to be joined instead of simply treating them as parts of a basename/filename to join together.
-
#cp(dest, options = {}) ⇒ Object
(also: #copy)
Copies self to
dest
using FileUtils.cp. -
#cp_r(dest, options = {}) ⇒ Object
(also: #copy_recursive)
Copies self to
dest
using FileUtils.cp_r. -
#each_parent_dir ⇒ Object
Traverses up the file system until a match is found that is described by
block
(that is, untilblock
yields a true value). -
#install(dest, options = {}) ⇒ Object
Copies/install self to
dest
using FileUtils.install. -
#mv(new_path, options = {}) ⇒ Object
(also: #move)
Moves self to
new_path
using FileUtils.mv. -
#parent_dirs(include_self = true) ⇒ Object
Returns an array of all path parts that are directories.
-
#split_all ⇒ Object
Better name? Would ‘dirs’ be better? ‘parents’?.
-
#split_all_s ⇒ Object
Better name? Would ‘dirs’ be better?.
-
#split_all_without_basename ⇒ Object
Returns all path parts except the last (the last part being the basename).
-
#touch ⇒ Object
Same as FileUtils.touch.
Class Method Details
Instance Method Details
#absolutize ⇒ Object
Returns a Pathname object representing the absolute path equivalent to self.
If self is already absolute, returns self. Otherwise, prepends Pathname.getwd (in other words, creates an absolute path relative to the current working directory.)
Pathname.new(‘.’).absolutize == Pathname.getwd # => true
91 92 93 94 95 96 97 |
# File 'lib/quality_extensions/pathname.rb', line 91 def absolutize if absolute? self else (Pathname.getwd + self).cleanpath end end |
#add_prefix(s) ⇒ Object
178 179 180 |
# File 'lib/quality_extensions/pathname.rb', line 178 def add_prefix(s) Pathname.new((dirname + s).to_s + basename.to_s) end |
#add_suffix(s) ⇒ Object
This is needed since <#Pathname> + <#String> treats both self and the string as a directory to be joined instead of simply treating them as parts of a basename/filename to join together.
Pathname.new(‘/tmp/some_file’) + ‘.suffix’
> Pathname.new(‘/tmp/some_file/.suffix’)
Pathname.new(‘/tmp/some_file’).add_suffix(‘.suffix’)
> Pathname.new(‘/tmp/some_file.suffix’)
Pathname.new(‘/tmp/some_file/’).add_suffix(‘.suffix’)
> Pathname.new(‘/tmp/some_file.suffix’)
174 175 176 |
# File 'lib/quality_extensions/pathname.rb', line 174 def add_suffix(s) Pathname.new(cleanpath.to_s + s) end |
#cp(dest, options = {}) ⇒ Object Also known as: copy
Copies self to dest
using FileUtils.cp.
See documentation for FileUtils.cp for a list of valid options
.
Returns Pathname object for dest
file.
129 130 131 132 |
# File 'lib/quality_extensions/pathname.rb', line 129 def cp(dest, = {}) FileUtils.cp self.to_s, dest.to_s, Pathname.new(dest) end |
#cp_r(dest, options = {}) ⇒ Object Also known as: copy_recursive
Copies self to dest
using FileUtils.cp_r. If self is a directory, this method copies all its contents recursively. If dest
is a directory, copies self to dest/src
.
See documentation for FileUtils.cp_r for a list of valid options
.
Returns Pathname object for dest
file.
141 142 143 144 |
# File 'lib/quality_extensions/pathname.rb', line 141 def cp_r(dest, = {}) FileUtils.cp_r self.to_s, dest.to_s, Pathname.new(dest) end |
#each_parent_dir ⇒ Object
Traverses up the file system until a match is found that is described by block
(that is, until block
yields a true value).
Yields each parent directory (including self if self.directory?) as a Pathname object, one at a time, until we get to root of the file system (absolutize is called to force it to be an absolute path) or the block indicates that it has found what it’s looking for.
find_ancestor will return as its return value the block’s return value as soon as the block’s return value evaluates to true (not nil or false). If no match is found, find_ancestor will return nil.
Examples:
git_dir = Pathname.getwd.each_parent_dir {|dir| path = dir + '.git'; break path if path.exist? }
git_dirs = []; Pathname.getwd.each_parent_dir {|dir| path = dir + '.git'; git_dirs << path if path.exist? }
To do: deprecate, merge docs with parent_dirs?
294 295 296 297 298 299 |
# File 'lib/quality_extensions/pathname.rb', line 294 def each_parent_dir parent_dirs.reverse.each do |part| ret = yield part end nil end |
#install(dest, options = {}) ⇒ Object
Copies/install self to dest
using FileUtils.install.
If src is not same as dest, copies it and changes the permission mode to mode. If dest is a directory, destination is dest/src.
FileUtils.install 'ruby', '/usr/local/bin/ruby', :mode => 0755, :verbose => true
FileUtils.install 'lib.rb', '/usr/local/lib/ruby/site_ruby', :verbose => true
Returns Pathname object for dest
file.
157 158 159 160 |
# File 'lib/quality_extensions/pathname.rb', line 157 def install(dest, = {}) FileUtils.install self.to_s, dest.to_s, Pathname.new(dest) end |
#mv(new_path, options = {}) ⇒ Object Also known as: move
Moves self to new_path
using FileUtils.mv.
See documentation for FileUtils.mv for a list of valid options
.
Returns Pathname object for new_file
file.
117 118 119 120 |
# File 'lib/quality_extensions/pathname.rb', line 117 def mv(new_path, = {}) FileUtils.mv self.to_s, new_path.to_s, Pathname.new(new_path) end |
#parent_dirs(include_self = true) ⇒ Object
Returns an array of all path parts that are directories. (If self.directory?
, self is included too, unless include_self
is false.)
This is similar to parts_without_basename except unlike parts_without_basename, which removes the last path (‘basename’) part no matter what, parent_dirs actually checks if the last path part is a directory and only removes it if it is not.
absolutize is called to force it to be an absolute path; so this method will not behave as advertized if the path is invalid or if the current working directory isn’t correct when the path is absolutized…
parent_dirs is not useful when used with fictional paths. It actually calls #directory? on the last path part, so the path must actually exist for this method to work as advertised. (You would might be advised to check #exist? before calling this method.) If you are confident that self is a directory, then you might want to use parts_without_basename.
Name ideas: parents, ancestors (but since it also includes self by default, I thought emphasizing dirs would be less misleading.)
224 225 226 227 228 |
# File 'lib/quality_extensions/pathname.rb', line 224 def parent_dirs(include_self = true) parents = absolutize.split_all.dup parents.pop if parents.any? && !(parents.last.directory? && include_self) parents end |
#split_all ⇒ Object
Better name? Would ‘dirs’ be better? ‘parents’?
Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory/basename represented in the path.
Similar to PHP’s pathinfo()?
188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/quality_extensions/pathname.rb', line 188 def split_all # Boundary condition for paths like '/usr' (['/', 'usr'] will be the result) if self.to_s == '/' [self] # Boundary condition for paths like 'usr' (we only want ['usr'], not ['.', 'usr']) elsif self.to_s == '.' [] else parent.split_all + [self] end end |
#split_all_s ⇒ Object
Better name? Would ‘dirs’ be better?
Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory represented in the path.
Pathname.new(‘dir1/dir2/base’).parts_s # => [‘dir1’, ‘dir2’, ‘base’]
Pathname.new(‘dir1/dir2/base’).parts_s.first # => ‘dir1’
Pathname.new(‘/dir1/dir2/base’).parts_s # => [‘/’, ‘dir1’, ‘dir2’, ‘base’]
Unlike split_all, this returns an array of strings rather than an array of Pathnames.
Similar to PHP’s pathinfo()?
247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/quality_extensions/pathname.rb', line 247 def split_all_s a, b = split a, b = a, b.to_s # Boundary condition for paths like '/usr' (['/', 'usr'] will be the result) if b == '/' [b] # Boundary condition for paths like 'usr' (we only want ['usr'], not ['.', 'usr']) elsif b == '.' [] else a.split_all_s + [b] end end |
#split_all_without_basename ⇒ Object
Returns all path parts except the last (the last part being the basename).
When there is only one part (as is the case, f.e., with Pathname(‘/’) or Pathname(‘file’)), it returns an empty array (rather than Pathname(‘/’) or Pathname(‘.’)).
Similar to split, but instead of only returning two parts ([dirname, basename]), returns an element for each directory represented in the path.
206 207 208 209 210 |
# File 'lib/quality_extensions/pathname.rb', line 206 def split_all_without_basename parents = split_all.dup parents.pop parents end |
#touch ⇒ Object
Same as FileUtils.touch
Options: noop verbose
Updates modification time (mtime) and access time (atime) of file(s) in list. Files are created if they don’t exist.
FileUtils.touch ‘timestamp’ FileUtils.touch Dir.glob(‘*.c’); system ‘make’
107 108 109 |
# File 'lib/quality_extensions/pathname.rb', line 107 def touch FileUtils.touch self.to_s end |