Module: MemoryIO::Util
- Defined in:
- lib/memory_io/util.rb
Overview
Defines utility methods.
Defined Under Namespace
Classes: FilePermission
Class Method Summary collapse
-
.file_permission(file) ⇒ MemoryIO::Util::FilePermission?
private
nilis returned if file does not exist or is inaccessible. -
.pack(val, b, endian = :little) ⇒ String
Pack an integer into
bbytes. -
.read_exactly(stream, size) ⇒ String
private
Read exactly
sizebytes fromstream. -
.safe_eval(str, **vars) ⇒ Integer?
Evaluate string safely.
-
.trim_libname(name) ⇒ String
Remove extension name (.so) and version in library name.
-
.underscore(str) ⇒ String
Convert input into snake-case.
-
.unpack(str, endian = :little) ⇒ Integer
Unpack a string into an integer.
Class Method Details
.file_permission(file) ⇒ MemoryIO::Util::FilePermission?
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns nil is returned if file does not exist or is inaccessible.
68 69 70 71 72 |
# File 'lib/memory_io/util.rb', line 68 def (file) return nil unless File.file?(file) FilePermission.new(file) end |
.pack(val, b, endian = :little) ⇒ String
Pack an integer into b bytes.
163 164 165 166 167 |
# File 'lib/memory_io/util.rb', line 163 def pack(val, b, endian = :little) bytes = Array.new(b) { |i| (val >> (i * 8)) & 0xff } bytes.reverse! if endian == :big bytes.pack('C*') end |
.read_exactly(stream, size) ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Read exactly size bytes from stream.
114 115 116 117 118 119 120 |
# File 'lib/memory_io/util.rb', line 114 def read_exactly(stream, size) str = stream.read(size) remain = str.nil? ? 0 : str.size raise ::EOFError, format('Requires 0x%x bytes, but only 0x%x bytes remain', size, remain) if remain < size str end |
.safe_eval(str, **vars) ⇒ Integer?
Evaluate string safely.
90 91 92 93 94 |
# File 'lib/memory_io/util.rb', line 90 def safe_eval(str, **vars) return str if str.is_a?(Integer) Dentaku::Calculator.new.store(vars).evaluate(str) end |
.trim_libname(name) ⇒ String
Remove extension name (.so) and version in library name.
186 187 188 189 190 191 192 |
# File 'lib/memory_io/util.rb', line 186 def trim_libname(name) return 'ld' if name.start_with?('ld-') type1 = '(-[\d.]+)?\.so$' type2 = '\.so.[\.\d]+$' name.sub(/#{type1}|#{type2}/, '') end |
.underscore(str) ⇒ String
Convert input into snake-case.
This method also converts '::' to '/'.
51 52 53 54 55 56 57 58 59 |
# File 'lib/memory_io/util.rb', line 51 def underscore(str) return '' if str.empty? str = str.gsub('::', '/') str.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2') str.gsub!(/([a-z\d])([A-Z])/, '\1_\2') str.downcase! str end |
.unpack(str, endian = :little) ⇒ Integer
Unpack a string into an integer.
139 140 141 142 |
# File 'lib/memory_io/util.rb', line 139 def unpack(str, endian = :little) bytes = endian == :little ? str.bytes.reverse : str.bytes bytes.reduce(0) { |s, c| (s * 256) + c } end |