Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
Pathname#+
# File tmp/rubies/ruby-4.1.0-preview1/pathname_builtin.rb, line 909
def +(other)
other = Pathname.new(other) unless Pathname === other
Pathname.new(plus(@path, other.path))
end
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:
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:
Pathname('/a/b') + 'c' # => #<Pathname:/a/b/c>
Extra component separators ('/') are removed:
Pathname('/a/b/') + 'c' # => #<Pathname:/a/b/c>
Extra current-directory components ('.') are removed:
Pathname('a') + '.' # => #<Pathname:a> Pathname('.') + 'a' # => #<Pathname:a> Pathname('.') + '.' # => #<Pathname:.>
Parent-directory components ('..') are:
-
Resolved, when possible:
ExamplePathname('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:
ExamplePathname('/') + '..' # => #<Pathname:/>
-
Retained, when needed:
ExamplePathname('..') + '..' # => #<Pathname:../..> Pathname('..') + '../a' # => #<Pathname:../../a>
When other specifies an absolute path (see absolute?), equivalent to Pathname(other.to_s):
Pathname('/a') + '/b/c' # => #<Pathname:/b/c>
Occurrences of '/', '.', and '..' are preserved:
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:
Pathname('/var') + 'nosuch:ever' # => #<Pathname:/var/nosuch:ever>