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.



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

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.



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

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.



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

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.



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

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).



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

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.



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

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