class Ronin::Support::Path

The {Path} class extends ‘Pathname` to allow representing directory traversal paths.

@api public

Attributes

separator[RW]

The separator to join paths together with

@return [String]

Public Class Methods

new(path,separator=File::SEPARATOR) click to toggle source

Initializes a new Path.

@param [String] path

The path.

@param [String] separator

The directory separator to use.
Calls superclass method
# File lib/ronin/support/path.rb, line 45
def initialize(path,separator=File::SEPARATOR)
  @separator = separator

  super(path)
end
root() click to toggle source

The root path.

@return [Path]

The root path.
# File lib/ronin/support/path.rb, line 57
def self.root
  new('/')
end
up(n,separator=File::SEPARATOR) click to toggle source

Creates a new path object for upward directory traversal.

@param [Integer, Array, Range] n

The number of directories to go up.

@param [String] separator

Path separator.

@return [Path]

The new path object.

@raise [ArgumentError]

A negative number was given as the first argument.

@example Generate a relative path that goes up 7 directories:

Path.up(7)
# => #<Ronin::Support::Path:../../../../../../..>

@example Generate multiple relative paths, going up 1 to 3 directories:

Path.up(1..3)
# => [#<Ronin::Support::Path:..>, #<Ronin::Support::Path:../..>,
#<Ronin::Support::Path:../../..>]
# File lib/ronin/support/path.rb, line 85
def self.up(n,separator=File::SEPARATOR)
  case n
  when Integer
    if n == 0
      separator
    elsif n > 0
      path = new('..',separator)
      path.join(*(['..'] * (n - 1)))
    else
      raise(ArgumentError,"negative argument")
    end
  when Enumerable
    n.map { |i| up(i) }
  else
    raise(ArgumentError,"The first argument of Path.up must be either an Integer or Enumerable")
  end
end

Public Instance Methods

/(*names)
Alias for: join
join(*names) click to toggle source

Joins directory names together with the path, but does not resolve the resulting path.

@param [Array] names

The names to join together.

@return [Path]

The joined path.

@example

Path.up(7).join('etc/passwd')
# => #<Ronin::Support::Path:../../../../../../../etc/passwd>
# File lib/ronin/support/path.rb, line 117
def join(*names)
  joined_path = if root? then String.new(encoding: Encoding::UTF_8)
                else          self.to_s
                end

  names.each do |name|
    name = name.to_s

    joined_path << @separator unless name.start_with?(@separator)
    joined_path << name       unless name == @separator
  end

  return self.class.new(joined_path,@separator)
end
Also aliased as: /