123456789_123456789_123456789_123456789_123456789_

Module: Gem::Util

Relationships & Source Files
Defined in: lib/rubygems/util.rb

Overview

This module contains various utility methods as module methods.

Class Method Summary

Class Method Details

.correct_for_windows_path(path)

Corrects Gem.path (usually returned by Gem::URI.parse().path on Windows), that comes with a leading slash.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 118

def self.correct_for_windows_path(path)
  if path[0].chr == "/" && path[1].chr.match?(/[a-z]/i) && path[2].chr == ":"
    path[1..-1]
  else
    path
  end
end

.deep_dup(obj)

This method is for internal use only.
Duplicates obj without Marshal, which cannot resolve Gem

constants from

the root box under Ruby::Box (RUBY_BOX=1). Hashes and arrays are copied recursively, anything else with a plain dup, so an object's internals stay shared with the original, as are hash keys. Shared references are not preserved, and a cyclic obj raises SystemStackError.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 101

def self.deep_dup(obj) # :nodoc:
  case obj
  when Hash then obj.to_h {|k, v| [k, deep_dup(v)] }
  when Array then obj.map {|e| deep_dup(e) }
  else
    begin
      obj.dup
    rescue TypeError
      obj
    end
  end
end

.glob_files_in_dir(glob, base_path)

Globs for entries matching glob inside of base_path, returning absolute paths to the matching files and directories. Unlike a plain Dir.glob with an interpolated path, glob metacharacters in base_path are not treated as part of the pattern. Matched entries are joined to base_path literally, so an entry starting with ~ is not expanded into a home directory, and no other normalization is applied to them.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 85

def self.glob_files_in_dir(glob, base_path)
  expanded_path = nil
  Dir.glob(glob, base: base_path).map! do |f|
    # File.join instead of File.expand_path, so that matched entries
    # starting with `~` are not expanded into the home directory
    File.join(expanded_path ||= File.expand_path(base_path), f)
  end
end

.gunzip(data)

Zlib::GzipReader wrapper that unzips data.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 10

def self.gunzip(data)
  require "zlib"
  require "stringio"
  data = StringIO.new(data, "r")

  gzip_reader = begin
                  Zlib::GzipReader.new(data)
                rescue Zlib::GzipFile::Error => e
                  raise e.class, e.inspect, e.backtrace
                end

  unzipped = gzip_reader.read
  unzipped.force_encoding Encoding::BINARY
  unzipped
end

.gzip(data)

Zlib::GzipWriter wrapper that zips data.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 29

def self.gzip(data)
  require "zlib"
  require "stringio"
  zipped = StringIO.new(String.new, "w")
  zipped.set_encoding Encoding::BINARY

  Zlib::GzipWriter.wrap zipped do |io|
    io.write data
  end

  zipped.string
end

.inflate(data)

A Zlib::Inflate#inflate wrapper

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 45

def self.inflate(data)
  require "zlib"
  Zlib::Inflate.inflate data
end

.popen(*command)

This calls IO.popen and reads the result

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 53

def self.popen(*command)
  IO.popen command, &:read
end

.traverse_parents(directory, &block)

Enumerates the parents of directory.

[ GitHub ]

  
# File 'lib/rubygems/util.rb', line 60

def self.traverse_parents(directory, &block)
  return enum_for __method__, directory unless block_given?

  here = File.expand_path directory
  loop do
    begin
      Dir.chdir here, &block
    rescue StandardError
      Errno::EACCES
    end

    new_here = File.expand_path("..", here)
    return if new_here == here # toplevel
    here = new_here
  end
end