Skip to content

Instantly share code, notes, and snippets.

View lwoodson's full-sized avatar

Lance Woodson lwoodson

View GitHub Profile
2.1.1 :001 > array = [1] * 100
=> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2.1.1 :002 > array.each_slice(array.size / 4).to_a
=> [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
2.1.1 :003 > arrays = array.each_slice(array.size / 4).to_a
=> [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1,
@lwoodson
lwoodson / CreatePdf.java
Last active August 29, 2015 14:05
Benchmarking PDF libs
package com.shippingeasy.javapdf;
import java.io.File;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.edit.*;
import org.apache.pdfbox.pdmodel.graphics.xobject.*;
class Backend
def called(klass, method, args, time)
clues_performance << marshall(klass, method, args, time)
end
private
def marshal(klass, method, args, time)
{
"code" => "#{klass}##{method}"
"args" => args.to_s
@lwoodson
lwoodson / receiver.py
Last active August 29, 2015 14:06
Ruby/Python Interop the easiest way
import sys
import json
raw = sys.stdin.read()
data = json.loads(raw)
data["response"] = "Howdy from python"
print json.dumps(data)
@lwoodson
lwoodson / sh
Last active August 29, 2015 14:06
> find /home/shipit/file_backup -type d | \
sed 's/\/home\/shipit\/file_backup\///' | \
xargs mkdir -p
> find /home/shipit/file_backup -type f | \
awk '{src = $0; dest = $0; gsub(/\/home\/shipit\/file_backup\//, "", dest); print src " " dest}' | \
xargs -I{} echo 'cp {}' > copy_en_masse
> chmod +x copy_en_masse
@lwoodson
lwoodson / cp_gem_assets
Created December 5, 2014 14:18
Copy gem assets as a deploy step
#!/bin/bash
# Command to copy assets from within a gem to a path in the
# deployed app's public directory so that they can be served
# in staging/production
error() {
echo "ERROR: $@"
echo
echo "USAGE: cp_gem_assets [gemname] [path_in_gem] [path_in_public]"
exit -1
}
common: &common
image: myapp
environment:
APPENV: production
ELASTIC_HOST: es01
ELASTIC_PORT: 9200
REDIS_HOST: r01
REDIS_PORT: 6379
myapp01:
@lwoodson
lwoodson / resque_run_inline.rb
Last active August 29, 2015 14:15
Resque run inline in specs
##
# Will run resque jobs inline within a spec so that
# if you need jobs to run to set up a state, you
# can simply invoke the methods that enqueue those
# jobs and everything is run by the test run
RSpec.shared_context "resque run inline" do
around(:each) do |example|
without_resque_spec do
inline = Resque.inline
Resque.inline = true
@lwoodson
lwoodson / postgresql.sql
Last active August 29, 2015 14:18
Postgres SQL procedure/trigger to stop deletes from a table
-- Create procedure
DROP FUNCTION IF EXISTS die_on_delete();
CREATE FUNCTION die_on_delete() RETURNS trigger AS $$
BEGIN
RAISE EXCEPTION 'DONT DELETE ME BRO!';
END;
$$ LANGUAGE plpgsql;
-- Create trigger
DROP TRIGGER IF EXISTS die_on_order_delete ON orders;
@lwoodson
lwoodson / net_instrumentation.rb
Created April 17, 2015 21:56
Instrument HTTP calls made with Net::HTTP
HTTP_LOG_PATH = File.expand_path("~/http.log")
FileUtils.rm_f(HTTP_LOG_PATH)
module NetInstrumentation
def self.included(base)
puts "Writing HTTP out to ~/http.log"
base.send(:alias_method, :__original_request, :request)
base.send(:alias_method, :request, :__logged_request)
end