Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

Pathname#/

(::Pathname | ::String | ::_ToStr other) -> ::Pathname
/(other)

Alias of #+

Returns a new Pathname object based on the content of self and other; argument other may be a String, a File, a Dir, or another Pathname:

Example
pn = Pathname('foo')     # => #<Pathname:foo>
pn + 'bar'               # => #<Pathname:foo/bar>
pn + File.new('LEGAL')   # => #<Pathname:foo/LEGAL>
pn + Dir.new('lib')      # => #<Pathname:foo/lib>
pn + Pathname('bar')     # => #<Pathname:foo/bar>

When other specifies a relative path (see relative?), it is combined with self to form a new pathname:

Example
Pathname('/a/b') + 'c' # => #<Pathname:/a/b/c>

Extra component separators ('/') are removed:

Example
Pathname('/a/b/') + 'c' # => #<Pathname:/a/b/c>

Extra current-directory components ('.') are removed:

Example
Pathname('a') + '.' # => #<Pathname:a>
Pathname('.') + 'a' # => #<Pathname:a>
Pathname('.') + '.' # => #<Pathname:.>

Parent-directory components ('..') are:

  • Resolved, when possible:

    Example
    Pathname('a')      + '..'      # => #<Pathname:.>
    Pathname('a/b')    + '..'      # => #<Pathname:a>
    Pathname('/')      + '../a'    # => #<Pathname:/a>
    Pathname('a')      + '../b'    # => #<Pathname:b>
    Pathname('a/b')    + '../c'    # => #<Pathname:a/c>
    Pathname('a//b/c') + '../d//e' # => #<Pathname:a//b/d//e>
    
  • Removed, when not needed:

    Example
    Pathname('/') + '..' # => #<Pathname:/>
    
  • Retained, when needed:

    Example
    Pathname('..') + '..'   # => #<Pathname:../..>
    Pathname('..') + '../a' # => #<Pathname:../../a>
    

When other specifies an absolute path (see absolute?), equivalent to Pathname(other.to_s):

Example
Pathname('/a') + '/b/c' # => #<Pathname:/b/c>

Occurrences of '/', '.', and '..' are preserved:

Example
Pathname('/a') + '//b//c/./../d' # => #<Pathname://b//c/./../d>

This method does not access the file system, so other need not represent an existing (or even a valid) file or directory path:

Example
Pathname('/var') + 'nosuch:ever' # => #<Pathname:/var/nosuch:ever>
See also #+ #<=> .pwd #== also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0