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) ⇒ String
Pack an integer into
bbytes. -
.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) ⇒ 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) ⇒ String
Pack an integer into b bytes. Little endian is used.
129 130 131 |
# File 'lib/memory_io/util.rb', line 129 def pack(val, b) Array.new(b) { |i| (val >> (i * 8)) & 0xff }.pack('C*') end |
.safe_eval(str, **vars) ⇒ Integer
Evaluate string safely.
87 88 89 90 91 92 93 |
# File 'lib/memory_io/util.rb', line 87 def safe_eval(str, **vars) return str if str.is_a?(Integer) # dentaku 2 doesn't support hex str = str.gsub(/0x[0-9a-zA-Z]+/) { |c| c.to_i(16) } Dentaku::Calculator.new.store(vars).evaluate(str) end |
.trim_libname(name) ⇒ String
Remove extension name (.so) and version in library name.
150 151 152 153 154 155 156 |
# File 'lib/memory_io/util.rb', line 150 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) ⇒ Integer
Unpack a string into an integer. Little endian is used.
109 110 111 |
# File 'lib/memory_io/util.rb', line 109 def unpack(str) str.bytes.reverse.reduce(0) { |s, c| (s * 256) + c } end |