Module: Gem::Util
- Defined in:
- lib/rubygems/util.rb
Overview
This module contains various utility methods as module methods.
Class Method Summary collapse
-
.correct_for_windows_path(path) ⇒ Object
Corrects
path(usually returned byGem::URI.parse().pathon Windows), that comes with a leading slash. -
.deep_dup(obj) ⇒ Object
- Duplicates
objwithout Marshal, which cannot resolve Gem constants from the root box under Ruby::Box (RUBY_BOX=1).
- Duplicates
-
.glob_files_in_dir(glob, base_path) ⇒ Object
Globs for entries matching
globinside ofbase_path, returning absolute paths to the matching files and directories. -
.gunzip(data) ⇒ Object
Zlib::GzipReader wrapper that unzips
data. -
.gzip(data) ⇒ Object
Zlib::GzipWriter wrapper that zips
data. -
.inflate(data) ⇒ Object
A Zlib::Inflate#inflate wrapper.
-
.popen(*command) ⇒ Object
This calls IO.popen and reads the result.
-
.traverse_parents(directory, &block) ⇒ Object
Enumerates the parents of
directory.
Class Method Details
.correct_for_windows_path(path) ⇒ Object
Corrects path (usually returned by Gem::URI.parse().path on Windows), that
comes with a leading slash.
118 119 120 121 122 123 124 |
# 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) ⇒ Object
- 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.
101 102 103 104 105 106 107 108 109 110 111 112 |
# 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) ⇒ Object
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.
85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
Zlib::GzipReader wrapper that unzips data.
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# 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) ⇒ Object
Zlib::GzipWriter wrapper that zips data.
29 30 31 32 33 34 35 36 37 38 39 40 |
# 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) ⇒ Object
A Zlib::Inflate#inflate wrapper
45 46 47 48 |
# File 'lib/rubygems/util.rb', line 45 def self.inflate(data) require "zlib" Zlib::Inflate.inflate data end |
.popen(*command) ⇒ Object
This calls IO.popen and reads the result
53 54 55 |
# File 'lib/rubygems/util.rb', line 53 def self.popen(*command) IO.popen command, &:read end |
.traverse_parents(directory, &block) ⇒ Object
Enumerates the parents of directory.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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 |