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
-
.correct_for_windows_path(path)
Corrects path (usually returned by
Gem::URI.parse().pathon Windows), that comes with a leading slash. -
.glob_files_in_dir(glob, base_path)
Globs for entries matching
globinside ofbase_path, returning absolute paths to the matching files and directories. -
.gunzip(data)
Zlib::GzipReaderwrapper that unzipsdata. -
.gzip(data)
Zlib::GzipWriterwrapper that zipsdata. -
.inflate(data)
A
Zlib::Inflate#inflatewrapper. -
.popen(*command)
This calls
IO.popenand reads the result. -
.traverse_parents(directory, &block)
Enumerates the parents of
directory. -
.deep_dup(obj)
Internal use only
- Duplicates
objwithout Marshal, which cannot resolve Gem constants from the root box under
Ruby::Box(RUBY_BOX=1).
- Duplicates
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.
.deep_dup(obj)
- Duplicates
objwithout 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.
# 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.
# File 'lib/rubygems/util.rb', line 85
def self.glob_files_in_dir(glob, base_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( ||= File.(base_path), f) end end
.gunzip(data)
Zlib::GzipReader wrapper that unzips data.
# 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.
# 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
# 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
# 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.
# File 'lib/rubygems/util.rb', line 60
def self.traverse_parents(directory, &block) return enum_for __method__, directory unless block_given? here = File. directory loop do begin Dir.chdir here, &block rescue StandardError Errno::EACCES end new_here = File.("..", here) return if new_here == here # toplevel here = new_here end end