Forked from kernelsmith/ruby_tips_tricks_one-liners.rb
Created
December 13, 2013 05:53
-
-
Save deadbits/7940302 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# One-liners (or one'ish-liners) | |
# | |
### CONVERT a hex file to hex string | |
# for_testing$ echo -n "DEADBEEFDEADBEEF" > tmp | |
File.open("tmp", "rb") {|f| [f.read].pack("H*")} | |
# => "\xDE\xAD\xBE\xEF\xDE\xAD\xBE\xEF" | |
### CONVERT to base64 (note .encode and .strict_encode can have different results) | |
require 'base64' and File.open("tmp", "rb") {|f| Base64.strict_encode64(f.read)} | |
# => "REVBREJFRUZERUFEQkVFRg==\n" | |
### sweet TIMESTAMP, nicely formatted & machine sortable, good for file names esp the 2nd one. | |
require 'time' # yes this is required. To access the .utc method iirc | |
Time.now.utc.iso8601 # => "2013-03-27T21:40:19Z" | |
Time.now.utc.iso8601.gsub(':','-') # => "2013-03-27T21-40-19Z" | |
### one line WEBSERVER, like python -m SimpleHTTPServer port | |
ruby -run -e httpd -- [-p port] DocumentRoot | |
### CONVERT 2 ARRAYS TO A HASH | |
a1 = [1,2,3] | |
a2 = ["one", "two", "three"] | |
h = Hash[a1.zip(a2)] | |
# => {1=>"one", 2=>"two", 3=>"three"} | |
# or as a true one-liner, tho more confusing: | |
Hash[ [1,2,3].zip(["one", "two", "three"]) ] | |
# this is especially useful when you are reading lines from a file, like a CSV file and want to map items to the column title: | |
columns = [:last, :first, :age] | |
CSV.read_lines do |line| | |
hashes << Hash[ columns.zip(line) ] # <= {:age=>69, :first=>"James", :last=>"Smith"} etc. | |
end | |
# | |
# Tips & Tricks | |
# | |
### basic usage of timeouts | |
require 'timeout' | |
timeout = 10 # in seconds | |
buffer = '' | |
io = SomeFakeIo.new | |
begin | |
Timeout.timeout(timeout) do | |
ret = nil | |
# do stuff that might timeout | |
io.read | |
end | |
return ret | |
rescue ::Timeout::Error #, Rex::TimeoutError as well if Metasploit (lib/rex/exceptions.rb) | |
puts "Timeout Error message" | |
return nil | |
rescue ::OtherException => e | |
raise e | |
ensure | |
io.close if io | |
end | |
### create zip files in memory | |
# credit: http://www.devinterface.com/blog/en/2010/02/create-zip-files-on-the-fly/ | |
def create_zip(file_list, filename = nil, &block) | |
if !file_list.blank? | |
# require 'time' is assumed from above | |
stamp = Time.now.utc.iso8601.gsub(':','-') || "tmp" | |
filename ||= "#{stamp}.zip" | |
Tempfile.open(stamp) do |t| | |
Zip::ZipOutputStream.open(t.path) do |z| | |
file_list.each do |f| | |
z.put_next_entry(f) | |
z.print IO.read(f) # this could use some error handling | |
end | |
end | |
# Do something with t now. If you're emailing this from Rails you can: | |
#send_file t.path, :type => 'application/zip',:disposition => 'attachment',:filename => file_name | |
# But I want to save it to disk, we can use a block form | |
if block_given? | |
yield t | |
else | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment