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#sub_ext

(::string replacement) -> ::Pathname
sub_ext(replacement) → new_pathname

Returns a new pathname whose path is the path in self, after specified changes:

Example
Pathname('t.tmp').sub_ext('.txt') # => #<Pathname:t.txt>     # Extension replaced.
Pathname('temp').sub_ext('.txt')  # => #<Pathname:temp.txt>  # Extension added.
Pathname('t.tmp').sub_ext('')     # => #<Pathname:t>         # Extension removed.
See also #sub #symlink? #sticky? #sysopen also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0