Class: Ronin::Path
Overview
The Path class extends `Pathname` to allow representing directory traversal paths.
Instance Attribute Summary (collapse)
-
- (Object) separator
The separator to join paths together with.
Class Method Summary (collapse)
-
+ (Path) root
The root path.
-
+ (Path) up(n, separator = File::SEPARATOR)
Creates a new path object for upward directory traversal.
Instance Method Summary (collapse)
-
- (Path) initialize(path)
constructor
A new instance of Path.
-
- (Path) join(*names)
(also: #/)
Joins directory names together with the path, but does not resolve the resulting path.
Constructor Details
- (Path) initialize(path)
A new instance of Path
32 33 34 35 36 |
# File 'lib/ronin/path.rb', line 32 def initialize(path) @separator = File::SEPARATOR super(path) end |
Instance Attribute Details
- (Object) separator
The separator to join paths together with
30 31 32 |
# File 'lib/ronin/path.rb', line 30 def separator @separator end |
Class Method Details
+ (Path) root
The root path.
46 47 48 |
# File 'lib/ronin/path.rb', line 46 def Path.root Path.new('/') end |
+ (Path) up(n, separator = File::SEPARATOR)
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
- (Path) join(*names) 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 |