[RFC] Path extensions

What do you think of these extensions to Path:

struct Path
  def with_stem(stem : String)
    raise("Cannot override stem in '#{self}'") if ends_with_separator?
    Path.new(dirname) / {stem, extension}.join
  end

  def with_extension(ext : String)
    raise("Extension cannot start with a dot: '#{ext}'") if ext.starts_with?(".")
    raise("Cannot override extension in '#{self}'") if ends_with_separator?
    Path.new(dirname) / {stem, ".", ext}.join
  end
end

ROOT = Path.new("/")
input_path = ROOT / "images" / "image01.png"
output_path = input_path.with_extension("jpg") # "/images/image01.jpg"

Basically these methods are supposed to make overriding stem and extension of an existing Path simpler. Particularly in applications or scripts that deal with files and e.g. convert one file to another, this can be helpful. E.g. it would prevent people from changing the extension using gsub and regex. The implementation above is of course just the minimal code to make it work. There also might be better names (replace_, override_, …).

WDYT?

2 Likes