Skip to content

Instantly share code, notes, and snippets.

View split.rb
require 'benchmark'
alias e puts
N = "\n"
N_TIMES = 550_000 # Run it n times.
TEST_STRING = ('abc,def,ghi,jkl,'+N) * 80
Benchmark.bm(24) { |x|
x.report("Testing: split(',')") {
View ghosttest.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"
View byte_buffer.rb
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*')
@bradland
bradland / script_base.rb
Last active May 3, 2017
Base Ruby shell scripting template. Uses std-lib only; parses options; traps common signals.
View script_base.rb
#!/usr/bin/env ruby
require 'ostruct'
require 'optparse'
## Embedded ScriptUtils library; because, scripting!
module ShellScriptUtils
Version = '2016.10.04.001' # YYYY.MM.DD.vvv
def status(msg, type=:info, indent=0, io=$stderr)
View datebench_ips.rb
#!/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)
View pry_long_time.rb
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'
@bradland
bradland / string_replacement.rb
Last active Aug 29, 2015
String replacement/removal method benchmarks.
View string_replacement.rb
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.
View audio_text.rb
require_relative 'lib/portaudio'
class AudioTest
include PortAudio
def initialize
PortAudio.init
@devices = get_devices
end
@bradland
bradland / unicode_box_drawing.rb
Created Dec 10, 2014
All unicode box drawing characters
View unicode_box_drawing.rb
('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
@bradland
bradland / rescue.rb
Last active Aug 29, 2015
Trap vs rescue for handling ctrl-C during script execution.
View rescue.rb
# 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