Skip to content

Instantly share code, notes, and snippets.

@bradland
bradland / base642ip
Created March 5, 2014 04:17
Convert IP address to/from base64 encoded format for BOOTP DHCP configuration in OS X Server (or other applications).
#!/usr/bin/env ruby
# Based on answer found here:
# http://stackoverflow.com/questions/7618598/unpack-base64-encoded-32bit-integer-representing-ip-address
require 'base64'
class App
def run(arg)
base642ip(arg)
@bradland
bradland / csvanalyze.rb
Last active August 29, 2015 14:07
Boilerplate CSV analysis script
#!/usr/bin/env ruby
require 'csv'
require 'ostruct'
require 'optparse'
require 'i18n'
require 'set' # this should only be required until i18n updates to new rev
require 'active_support/core_ext/string/inflections'
I18n.enforce_available_locales = true # I18n spams warn unless this is set
@bradland
bradland / rescue.rb
Last active August 29, 2015 14:08
Trap vs rescue for handling ctrl-C during script execution.
# Using a block allows you to limit the scope of the Interrupt handling code,
# as well as providing the opportunity to provide a meaningful exit code. For
# more information on exit codes, see: http://tldp.org/LDP/abs/html/exitcodes.html
begin
sleep 10
rescue Interrupt
$stderr.puts "Stopped sleeping..."
exit 130
end
@bradland
bradland / unicode_box_drawing.rb
Created December 10, 2014 21:16
All unicode box drawing characters
('2500'.to_i(16)..'257F'.to_i(16)).map { |i| [i.to_s(16), [i].pack('U')] }.each { |c| puts "%s: %s" % [c[0], c[1]] };0
require_relative 'lib/portaudio'
class AudioTest
include PortAudio
def initialize
PortAudio.init
@devices = get_devices
end
@bradland
bradland / string_replacement.rb
Last active August 29, 2015 14:12
String replacement/removal method benchmarks.
require 'benchmark'
alias e puts
N = "\n"
N_TIMES = 550_000 # Run it n times.
e 'We will next compare the elimination of the character "f"'
e 'by comparing the methods .delete .gsub and .tr'+N+N
# Our Test String comes next.
class Foo
attr_accessor :baz, :qux, :quux, :corge, :grault, :garply, :waldo, :fred, :plugh, :xyzzy, :thud
def initialize(bar)
@bar = bar
@baz = 'baz'
@qux = 'qux'
@quux = 'quux'
@corge = 'corge'
@grault = 'grault'
@garply = 'garply'
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'time'
if defined?(JRUBY_VERSION)
JODA_FMT = org.joda.time.format::DateTimeFormat.for_pattern("Y-M-d H:m:s Z")
end
def joda_time(time)
class ByteBuffer
attr_reader :bytes
# A ByteBuffer accepts encoded string buffers and unpacks them to an
# internal array of bytes stored using fixnum so that binary operations can
# be easily performed.
def initialize(buffer, mode)
case mode
when 'a' # arbitrary string; converts to ascii char code (this is faster than String#ord)
@bytes = buffer.unpack('C*')
/* ghosttest.c: GHOST vulnerability tester */
/* Credit: http://www.openwall.com/lists/oss-security/2015/01/27/9 */
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define CANARY "in_the_coal_mine"