Sha256: 5b14d17d6b5c6bf10f432e0b09d48745bee42a7bcc4034d9437aab038d3ccbad

Contents?: true

Size: 1.01 KB

Versions: 10

Compression:

Stored size: 1.01 KB

Contents

class File

  # Takes a file name string and returns or changes its extension.
  #
  # Without a new extension argument, returns the extension of the
  # file name. In this respect #ext is like #extname, but unlike
  # #extname it does not include the dot prefix.
  #
  # With a new extension argument, changes the exension of the file
  # name to the new extension and returns it.
  #
  # Examples
  #
  #   File.ext('file.rb')          # => 'rb'
  #   File.ext('file.rb', 'txt')   # => 'file.txt'
  #   File.ext('file.rb', '.txt')  # => 'file.txt'
  #   File.ext('file.rb', '')      # => 'file'
  #
  # This method can be used with String#file for more object-oriented notation:
  #
  #   'file.rb'.file.ext('txt')    # => 'file.txt'
  #
  # CREDIT: Lavir the Whiolet

  def self.ext(filename, new_ext=nil)
    old_ext = extname(filename)
    if new_ext == nil
      old_ext.sub(/^\./, '')
    else
      new_ext = '.' + new_ext unless (new_ext.empty? || new_ext[0,1] == '.')
      filename.chomp(old_ext) + new_ext
    end
  end

end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/file/ext.rb
facets-3.1.0 lib/core/facets/file/ext.rb
facets-3.0.0 lib/core/facets/file/ext.rb
facets-2.9.3 lib/core/facets/file/ext.rb
facets-2.9.2 lib/core/facets/file/ext.rb
facets-2.9.2 src/core/facets/file/ext.rb
facets-2.9.1 lib/core/facets/file/ext.rb
facets-2.9.0 lib/core/facets/file/ext.rb
facets-2.9.0.pre.2 lib/core/facets/file/ext.rb
facets-2.9.0.pre.1 lib/core/facets/file/ext.rb