module RemoveFileSuffix

#

require 'remove_file_suffix/constants.rb'

#

Constants

REMOVE_UNUSUAL_SUFFIXES
#

REMOVE_UNUSUAL_SUFFIXES

If the following constant is set to true, then we will also get rid of e. g. “-source” names.

#

Public Class Methods

[](i) click to toggle source
#

RemoveFileSuffix[]

#
# File lib/remove_file_suffix/remove_file_suffix.rb, line 63
def self.[](i)
  remove_file_suffix(i)
end
remove_file_suffix(i) click to toggle source
#

RemoveFileSuffix.remove_file_suffix(i)

Class method here. The order should be kept, IMO. We get rid of the file suffix.

Note that .sub() for .tar should come after .gz and .xz, because .tar is typically coming before these two as well, such as in a “.tar.gz” file. Hence we first eliminate .gz in the following batch .sub listing.

Would it not be easier to just use File.basename() instead? We will try this since Jan 2014. Hmm nope, won't work.

#
# File lib/remove_file_suffix/remove_file_suffix.rb, line 33
def self.remove_file_suffix(i)
  i = i.dup if i.frozen?
  i = i.sub(/\.bz2$/, ''). # Get rid of .bz2
        sub(/\.gz$/,  ''). # Get rid of .gz
        sub(/\.zip$/, ''). # Get rid of .zip
        sub(/\.xz$/,  ''). # Get rid of .xz
        sub(/\.Z$/,   ''). # Get rid of .Z
        sub(/\.tar$/, ''). # Get rid of .tar
        sub(/\.tgz$/, ''). # Get rid of .tgz
        sub(/\.gem$/, ''). # Get rid of .gem
        sub(/\.yml$/, ''). # Get rid of .yml
        sub(/\.js$/, '')   # Get rid of .js
  if REMOVE_UNUSUAL_SUFFIXES
    i.sub!(/-source/,'')
  end
  return i
end

Public Instance Methods

remove_file_suffix(i) click to toggle source
#

remove_file_suffix

This method will properly remove a file suffix.

#
# File lib/remove_file_suffix/remove_file_suffix.rb, line 56
def remove_file_suffix(i)
  RemoveFileSuffix.remove_file_suffix(i)
end