Module: Nuggets::File::ExtMixin

Included in:
File
Defined in:
lib/nuggets/file/ext_mixin.rb

Instance Method Summary collapse

Instance Method Details

#chomp_ext(path, ext = extname(path)) ⇒ Object

call-seq:

File.chomp_ext(path) -> aString

Returns a copy of path with its file extension removed.



36
37
38
# File 'lib/nuggets/file/ext_mixin.rb', line 36

def chomp_ext(path, ext = extname(path))
  path.chomp(ext)
end

#chomp_ext!(path, ext = extname(path)) ⇒ Object

call-seq:

File.chomp_ext!(path) -> aString | nil

Modifies path in place as described for #chomp_ext, returning path, or nil if no modifications were made.



45
46
47
# File 'lib/nuggets/file/ext_mixin.rb', line 45

def chomp_ext!(path, ext = extname(path))
  path.chomp!(ext)
end

#set_ext(path, new_ext, ext = extname(path)) ⇒ Object

call-seq:

File.set_ext(path, new_ext) -> aString

Returns a copy of path with its file extension removed and new_ext appended. Like #sub_ext, but also applies when file extension is not present.



73
74
75
# File 'lib/nuggets/file/ext_mixin.rb', line 73

def set_ext(path, new_ext, ext = extname(path))
  chomp_ext(path, ext) << new_ext
end

#set_ext!(path, new_ext, ext = extname(path)) ⇒ Object

call-seq:

File.set_ext!(path, new_ext) -> aString

Modifies path in place as described for #set_ext, returning path.



82
83
84
# File 'lib/nuggets/file/ext_mixin.rb', line 82

def set_ext!(path, new_ext, ext = extname(path))
  chomp_ext!(path, ext); path << new_ext
end

#sub_ext(path, new_ext, ext = extname(path)) ⇒ Object

call-seq:

File.sub_ext(path, new_ext) -> aString

Returns a copy of path with its file extension replaced with new_ext (if present; see also #set_ext).



54
55
56
# File 'lib/nuggets/file/ext_mixin.rb', line 54

def sub_ext(path, new_ext, ext = extname(path))
  sub_ext!(_path = path.dup, new_ext, ext); _path
end

#sub_ext!(path, new_ext, ext = extname(path)) ⇒ Object

call-seq:

File.sub_ext!(path, new_ext) -> aString | nil

Modifies path in place as described for #sub_ext, returning path, or nil if no modifications were made.



63
64
65
# File 'lib/nuggets/file/ext_mixin.rb', line 63

def sub_ext!(path, new_ext, ext = extname(path))
  path << new_ext if chomp_ext!(path, ext)
end