Method: FilePath#replace_extension
- Defined in:
- lib/filepath/filepath.rb
#replace_extension(new_ext) ⇒ FilePath #replace_extension ⇒ FilePath Also known as: replace_ext, sub_ext
Replaces or removes the file extension.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
# File 'lib/filepath/filepath.rb', line 329 def replace_extension(new_ext) # FIXME: accept block orig_filename = filename.to_s if !self.extension? if new_ext.nil? new_filename = orig_filename else new_filename = orig_filename + '.' + new_ext end else if new_ext.nil? pattern = /\.[^.]*?\Z/ new_filename = orig_filename.sub(pattern, '') else pattern = Regexp.new('.' + extension + '\\Z') new_filename = orig_filename.sub(pattern, '.' + new_ext) end end segs = @segments[0..-2] segs << new_filename return FilePath.new(segs) end |