Class: Ronin::Path
Overview
The Path class extends Pathname
to allow representing directory
traversal paths.
Instance Attribute Summary collapse
-
#separator ⇒ Object
The separator to join paths together with.
Class Method Summary collapse
-
.root ⇒ Path
The root path.
-
.up(n, separator = File::SEPARATOR) ⇒ Path
Creates a new path object for upward directory traversal.
Instance Method Summary collapse
-
#initialize(path) ⇒ Path
constructor
A new instance of Path.
-
#join(*names) ⇒ Path
(also: #/)
Joins directory names together with the path, but does not resolve the resulting path.
Constructor Details
Instance Attribute Details
#separator ⇒ Object
The separator to join paths together with
30 31 32 |
# File 'lib/ronin/path.rb', line 30 def separator @separator end |
Class Method Details
.root ⇒ Path
The root path.
46 47 48 |
# File 'lib/ronin/path.rb', line 46 def Path.root Path.new('/') end |
.up(n, separator = File::SEPARATOR) ⇒ Path
Creates a new path object for upward directory traversal.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ronin/path.rb', line 76 def self.up(n,separator=File::SEPARATOR) case n when Integer if n == 0 return separator elsif n < 0 raise(ArgumentError,"negative argument") end path = new('..') path.separator = separator dirs = (['..'] * (n-1)) return Path.new(path.join(*dirs)) when Enumerable return n.map { |i| up(i) } else raise(ArgumentError,"The first argument of Path.up must be either an Integer or Enumerable") end end |
Instance Method Details
#join(*names) ⇒ Path Also known as: /
Joins directory names together with the path, but does not resolve the resulting path.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/ronin/path.rb', line 114 def join(*names) names.map! { |name| name.to_s } # filter out errant directory separators names.reject! { |dir| dir == @separator } # join the path sub_path = names.join(@separator) path = if root? # prefix the root dir self.to_s + sub_path else # join the path with the sub-path [self.to_s, sub_path].join(@separator) end return self.class.new(path) end |