require 'pathname' class Pathname # create a new Pathname object for path, any backslashes are converted to /, # if Pathname object is passed in then returns this existing pathname object def self.for_path(path) return path if path.is_a? Pathname Pathname.new(path.gsub('\\','/')) end # return the concatenated path, cleaning up any backslashes, ignores nulls or empty pathnames def self.join(*args) pathnames = args.collect { |a| Pathname.for_path(a) } pathnames.inject { |fullpath, p| self.safe_concat(fullpath, p) } end # prepends a pathname to existing, ignores nulls or empty pathnames def self.safe_concat(prepend_pn, pathname) return nil if ((prepend_pn.nil? || prepend_pn.to_s.empty?) && (pathname.nil? || pathname.to_s.empty?)) #both are nil or empty return prepend_pn if (pathname.nil? || pathname.to_s.empty?) return pathname if (prepend_pn.nil? || prepend_pn.to_s.empty?) prepend_pn+pathname end # return the path string without the extension def path_no_ext dirname = self.dirname ext = self.extname base = self.basename(ext) presult = (dirname.to_s.empty?) ? base : dirname+base presult.to_s end end