Class: Ronin::Path

Inherits:
Pathname show all
Defined in:
lib/ronin/path.rb

Overview

The Path class extends Pathname to allow representing directory traversal paths.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Path

Returns 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

#separatorObject

The separator to join paths together with



30
31
32
# File 'lib/ronin/path.rb', line 30

def separator
  @separator
end

Class Method Details

.rootPath

The root path.

Returns:

  • (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.

Examples:

Generate a relative path that goes up 7 directories.

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

Generate multiple relative paths, going up 1 to 3 directories.

Path.up(1..3)
# => [#<Ronin::Path:..>, #<Ronin::Path:../..>,
#<Ronin::Path:../../..>]

Parameters:

  • n (Integer, Array, Range)

    The number of directories to go up.

  • separator (String) (defaults to: File::SEPARATOR)

    Path separator.

Returns:

  • (Path)

    The new path object.

Raises:

  • (ArgumentError)

    A negative number was given as the first argument.



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.

Examples:

Path.up(7).join('etc/passwd')
# => #<Ronin::Path:../../../../../../../etc/passwd>

Parameters:

  • names (Array)

    The names to join together.

Returns:

  • (Path)

    The joined 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